我收到了这些奇怪的错误,我一直在上下代码,注释和重写,并在谷歌上搜索所有的东西。也许你们会看到我没有看到的东西:
$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语句,但由于某种原因,它不起作用。
谢谢!
试着在搜索词($like变量)周围加上单引号。
例如:(gsa_committees.committee LIKE'$LIKE')
您需要将变量用引号括起来才能工作:
WHERE gsa_committees.committee LIKE '$like';
请参阅有关字符串比较函数的参考文档。
看起来缺少引号:
"WHERE gsa_committees.committee LIKE '$like' ";
好的,我明白了。这篇文章的答案解决了我的问题:多个表上的MYSQLI SQL查询失败
一旦我分配了表t1、t2等并执行了INNER JOIN,结果就如预期的那样出现了,分别是%$search%或$search。谢谢大家!