doulian7252 2015-05-31 22:35
浏览 57
已采纳

PHP / MYSQL查询失败但没有错误

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
  • 写回答

1条回答 默认 最新

  • dongmou5628 2015-05-31 22:56
    关注

    Sorry I'm not so experienced with php alone, but are you sure this returns a Boolean?

    $result = $conn->query($sql);
    

    If not can you remove that if statement?

    I wrote it as an answer because sadly I can't comment yet.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题