-1

我遇到了这些奇怪的错误,我一直在上下代码,评论和重写,并在谷歌上搜索所有的东西。也许你们会看到我没有看到的:

$mysqli = new mysqli('host','login','passwd','db');
if($mysqli->connect_errno > 0){ die('Cannot connect: '. $mysqli->connect_error); }
// See if there is one term or multiple terms
if (count($search) == 1) {
// If one term, search for that
$like = $search[0];
$stmt = "SELECT 
            gsa_committees.id,
            gsa_committees.committee,
            gsa_committees.appointer,
            gsa_committees.representatives,
            gsa_committees.contact,
            gsa_committees.category,
            gsa_committees.attachments,
            gsa_committees.labels,
            gsa_committee_reports.committee,
            gsa_committee_reports.title,
            gsa_committee_reports.author,
            gsa_committee_reports.link,
            gsa_funds.id,
            gsa_funds.fund,
            gsa_funds.attachments,
            gsa_funds.labels,
            gsa_meeting_minutes.title,
            gsa_meeting_minutes.link,
            gsa_officers.office,
            gsa_officers.dept,
            gsa_officers.name,
            gsa_representatives.program_dept,
            gsa_representatives.representatives,
            gsa_representatives.alternate
        FROM 
            gsa_committees,
            gsa_committee_reports,
            gsa_funds,
            gsa_meeting_minutes,
            gsa_officers,
            gsa_representatives
        WHERE 
            (gsa_committees.committee LIKE $like) AND   
                             gsa_committees.committee IS NOT NULL";
}
if(!$result = $mysqli->query($stmt)){ die('Bad query: '. $mysqli->error); }

这给了我这个错误信息:

Bad query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%ARCHAC%) AND gsa_committees.committee IS NOT NULL' at line 34

我知道这不是真的。如果我将 las 部分更改为:

WHERE gsa_committees.committee LIKE $like";

我收到此错误消息:

Bad query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%ARCHAC%' at line 34

在我看过的所有地方,字符串"%".search."%"似乎都是正确的方法,但我的服务器似乎不喜欢这里。

有趣的旁注:我在同一服务器上的另一个页面上有一个不同的 LIKE 语句,这只是由于某种原因不起作用。

谢谢!

4

4 回答 4

0

看起来好像缺少引号:

"WHERE gsa_committees.committee LIKE '$like' ";
于 2013-08-13T03:05:54.217 回答
0

尝试在搜索词周围加上单引号($like 变量)。

例如:(gsa_committees.committee LIKE '$like')

于 2013-08-13T03:01:06.463 回答
0

您需要将变量括在引号中才能正常工作:

WHERE gsa_committees.committee LIKE '$like';

请参阅字符串比较函数的参考文档。

于 2013-08-13T03:01:15.457 回答
-2

好,我知道了。这篇文章的答案解决了我的问题: MYSQLI SQL query over multiple tables failed

一旦我分配了表 t1、t2 等并进行了 INNER JOIN,结果就如预期的那样出现,带有 %$search% 或 $search。谢谢大家!

于 2013-08-29T17:55:03.263 回答