对于php和mysql,我必须说我还是个孩子,但我每天都在学习和改进你们的帮助。
是的,我使用MySQL在PHP中创建了一个搜索引擎,并且在某种程度上,它正在运行,但我遇到的挑战是当我使用多个关键字搜索“David Smith”时出现PDOException
错误消息但只有'大卫'或'史密斯',我没有任何错误。
老实说,我不知道在哪里弄错了。我使用wampserver PDO db connection
作为我的数据库。下面是代码示例。
//search button name and id is 'search'
$search_exploded = explode (" ", $search);
foreach($search_exploded as $search_each)
{
for($x=0; $x<=0; $x++ );
if($x==1)
$construct .="UserID LIKE '%$search_each%' or Requestor LIKE '%$search_each%' or EmploymentType LIKE '%$search_each%'";
else
$construct .="AND UserID LIKE '%$search_each%' AND Requestor LIKE '%$search_each%' AND EmploymentType LIKE '%$search_each%'";
}
$sql = "SELECT COUNT(*) as num FROM ".TBL_STUDENTS." WHERE $construct";
$result = $database->connection->prepare($sql);
$result->execute(array($construct));
就像我说的那样,当我使用像“大卫”这样的单个关键字进行搜索时,它可以正常运行,但当我搜索“David Smith”之类的内容时,我会收到错误。
以下是我收到的错误消息:
Fatal error: Uncaught exception '`PDOException`' with message '`SQLSTATE[42000]`:
Syntax error or access violation: 1064 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 'UserID LIKE '%Gift%' or Requestor LIKE `%Gift%` or EmploymentType LIKE `%Gift%` at line 3'
in `C:\wamp\www\ccnl\script\search_db.php` on line 108
该行是以下代码
$result->execute(array($construct));
答案 0 :(得分:0)
尝试使用数组,然后将其插入: -
//search button name and id is 'search'
$search_exploded = explode (" ", $search);
$construct = array();
foreach($search_exploded as $search_each)
{
$construct[] =" (UserID LIKE '%$search_each%' or Requestor LIKE '%$search_each%' or EmploymentType LIKE '%$search_each%') ";
}
$sql = "SELECT COUNT(*) as num FROM ".TBL_STUDENTS." WHERE ".implode(" OR ", $construct);
$result = $database->connection->prepare($sql);
$result->execute(array($construct));
答案 1 :(得分:0)
放一个空格
$construct .="AND Use
在和之前面对这样:
$construct .=" AND Use