したがって、MySQL データベースのエントリを検索する次の関数があります。
<?php
private function SearchContributors($search)
{
$search_pieces = explode(' ', $search);
if (count($search_pieces) == 1 )
{
$this->db->like('firstname', $search);
$this->db->or_like('lastname', $search);
$result = $this->db->get(); //the line from the error message below
}
//Other stuff for 2 and more pieces
return $result;
}
?>
この機能を 2 回使用します。
ケース Aはユーザーが開始した検索で、URL から検索クエリを取得します ( domain.com/contributors/?x=paul
)。これはうまくいきます。
<?php
if (isset($_GET['x']))
{
$x = $_GET['x'];
$result = $this->SearchContributors($x);
}
?>
ケース Bは、ユーザーが無効なスラッグ名 (domain.com/contributors/paul
の代わりにdomain.com/contributors/pauline-surname
) を入力し、検索クエリを直接取得した場合のバックアップです。
<?php
$this->db->where('slug', $slug);
$result = $this->db->get();
if ($result->num_rows() == 0)
{
$x = str_replace('-', ' ', $slug);
$result = $this->SearchContributors($x);
}
?>
これにより、MySQL 構文エラーが返されました。
エラー番号: 1064
SQL 構文にエラーがあります。MySQL サーバーのバージョンに対応するマニュアルを参照して、2 行目の 'WHERE
firstname
LIKE '%paul%' ORlastname
LIKE '%paul%''付近で使用する正しい構文を確認してください。SELECT * WHERE
firstname
LIKE '%paul%' ORlastname
LIKE '%paul%'ファイル名: /www/htdocs/w00a94ee/c/controllers/contributors.php
ライン番号: 23
関数はどちらの場合もまったく同じ文字列を取得するのにpaul
、なぜ機能しないのでしょうか?
//編集
function __construct()
{
parent::__construct();
$this->load->database('databasename');
$this->db->from('tablename');
}