My mySQLi query is failing but the error is blank. Can anybody tell me what the problem is, or how I can output info on the error?
Heres my PHP code:
// 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";
}
And the resulting SQL query looks like this:
SELECT *, ((`words` LIKE '%word1%') + (`words` LIKE '%word2%') + (`words` LIKE '%word3%')) AS `numMatches` FROM `words`HAVING `numMatches` >= 3 ORDER BY `numMatches` DESC