我的mySQLi查询失败,但错误是空白的。任何人都可以告诉我问题是什么,或者我如何输出错误信息?
继承我的PHP代码:
// Create connection
$conn = new mysqli($hostname, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['in_wordarray']))
{
$words = $_POST['in_wordarray'];
$sql = "SELECT *, (";
$i = 0;
foreach ($words as $value) {
if($i == 0) {
$sql .= "(`words` LIKE '%$value%')";
} else {
$sql .= " + (`words` LIKE '%$value%')";
}
$i++;
}
$sql .= ") AS `numMatches` FROM `mytable` HAVING `numMatches` >= 3 ORDER BY `numMatches` DESC";
//echo $sql;
$result = $conn->query($sql);
if ($result === TRUE) {
$text_result_array = array();
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$text_result_array[] = $row;
}
// Encode the response
echo json_encode($text_result_array);
}
} else {
echo $conn->error;
}
} else {
echo "Bad Input";
}
生成的SQL查询如下所示:
SELECT *, ((`words` LIKE '%word1%') + (`words` LIKE '%word2%') + (`words` LIKE '%word3%')) AS `numMatches` FROM `words`HAVING `numMatches` >= 3 ORDER BY `numMatches` DESC
答案 0 :(得分:3)
抱歉,我没有单独使用php的经验,但你确定这会返回一个布尔值吗?
$result = $conn->query($sql);
如果没有,你可以删除if语句吗?
我把它写成答案,因为遗憾的是我还没有发表评论。