【问题标题】:Why is no records appearing when try to search for terms?为什么尝试搜索词条时没有出现记录?
【发布时间】:2012-06-29 12:14:07
【问题描述】:

好的,这很奇怪,但我会解释发生了什么。

在 SQL 中,假设我想使用 LIKE 语句在数据库中查找 2 个术语(AAA、AAB),能够找到这些术语结果的查询如下:

SELECT q.QuestionContent
FROM Question q
WHERE q.QuestionContent LIKE '%AAA%'
OR q.QuestionContent LIKE '%AAB%'
GROUP BY q.QuestionId, q.SessionId
ORDER BY IF( q.QuestionContent LIKE '%AAA%', 1, 0 ) , IF( q.QuestionContent LIKE '%AAB%', 1, 0 )

所以我知道 SQL 有效。所以我想做的是在 MYSQLi 中包含这个查询。唯一的区别是用户可以在搜索框中输入他们的术语,然后提交搜索框。因此,用户可以输入 1 个术语、2 个术语、3 个术语等,可以是任意数量的术语。

下面是我创建的 MYSQLi 代码以便能够做到这一点:

        <form action="previousquestions.php" method="get">
              <p>Search: <input type="text" name="questioncontent" value="<?php echo $questioncontent; ?>" onchange="return trim(this)" /></p>
              <p><input id="searchquestion" name="searchQuestion" type="submit" value="Search" /></p>
              </form>


        <?php 

        if (isset($_GET['searchQuestion'])) {

        $searchquestion = $questioncontent;
        $terms = explode(" ", $searchquestion);

        $questionquery = "SELECT q.QuestionContent FROM Question q WHERE ";

        $i=0;

        $whereArray = array();
        $orderByArray = array();
        $orderBySQL = "";
        $paramString = "";

        //loop through each term
        foreach ($terms as &$each) {
            $i++;
            //if only 1 term entered then perform this LIKE statement
            if ($i == 1){
                $questionquery .= "q.QuestionContent LIKE ? ";
            } else {
                //If more than 1 term then add an OR statement
                $questionquery .= "OR q.QuestionContent LIKE ? ";
                $orderBySQL .= ",";
            }

            $orderBySQL .= "IF(q.QuestionContent LIKE ? ,1,0)"; 

            $whereArray[] = "%" . $each . "%";
            $orderByArray[] = "%" . $each . "%"; 

            $paramString .= "ss";
        }  

        $questionquery .= "GROUP BY q.QuestionId, q.SessionId ORDER BY " . $orderBySQL; 
        $stmt=$mysqli->prepare($questionquery)or die($mysqli->error); 
function makeValuesReferenced(&$arr){ 
    $refs = array(); 
    foreach($arr as $key => $value) 
        $refs[$key] = &$arr[$key]; 
    return $refs; 

}

call_user_func_array(array($stmt, 'bind_param'), makeValuesReferenced(array_merge((array)$paramString,$whereArray, $orderByArray)));

    $stmt->execute();
    $stmt->bind_result($dbQuestionContent); 
    $questionnum = $stmt->num_rows();
    echo $questionquery;
    echo $paramString;

//OUTPUT RECORDS

        if (empty($questioncontent)){
    echo "Please enter in a phrase in the text box in able to search for a question";
}

        else if($questionnum ==0){
    echo "<p>Sorry, No Questions were found from this Search</p>";
    }
    else{

            $output = "";
    $output .= "
        <table border='1' id='resulttbl'>
          <tr>
          <th class='questionth'>Question</th>
          </tr>
    ";
            while ($stmt->fetch()) {
    $output .= "
          <tr>
          <td class='questiontd'>{$dbQuestionContent['QuestionContent']}</td>
          </tr>";
            }
            $output .= "        </table>";

            echo $output;

      }
}
            ?>

问题在于,如果用户输入了正确的术语,它不会显示数据库中包含这些术语的记录。它不输出任何东西。这没有任何意义,因为假设我在搜索框“AAA AAB”中输入了 2 个字词,当我回显查询和参数时,它似乎是正确的,因为它输出如下:

查询输出:

SELECT q.QuestionContent FROM Question q WHERE q.QuestionContent LIKE ? OR q.QuestionContent LIKE ? GROUP BY q.QuestionId, q.SessionId ORDER BY IF(q.QuestionContent LIKE ? ,1,0),IF(q.QuestionContent LIKE ? ,1,0)

参数输出:

ssss

所以我的问题是,如果查询正确且参数数量正确,发生了什么导致搜索成功时没有记录出现?

目前我收到一条警告:

警告:mysqli_stmt::bind_param() [mysqli-stmt.bind-param]: 类型定义字符串中的元素数与第 87 行......中的绑定变量数不匹配

需要做什么来解决这个警告?

【问题讨论】:

  • 这是什么,您第 10 次或第 13 次发布此确切代码?我已经数不清了。人们给了你很多建议,而你继续忽略它们。如果你没有得到你想要的答案,也许你应该重新考虑你的设计
  • @Kris ???我只问过一次这个问题,并且第二次发布了这个代码,你确定你没有把我误认为是有类似代码的其他人吗?
  • 那为什么这段代码看起来和stackoverflow.com/questions/11147669/…几乎一模一样,我猜它是在不同的用户名下但是来吧..
  • @Kris 哈哈,我认为有人发布相同代码的原因是因为我们三个人在一个小组中一起工作。我认为我们的一个小组成员也必须有一个 SO 帐户,并就相同的代码提出了一个问题,但不是我 :)
  • 哦,好的。对不起.. 我现在至少认出了这里的代码 7 次

标签: php mysqli


【解决方案1】:

您收到此错误的原因是因为您只传入了 1 个元素.. 一个数组。

$stmt->bind_param($paramString, array_merge($whereArray, $orderByArray));

它需要看起来像这样

$stmt->bind_param($paramString, $param1, $param2, $param3 ...);

$paramString 中的每个 s 都需要有一个单独的参数。 现在显然这有点困难,因为它可以是可变的数量。 所以替换

$stmt->bind_param($paramString, );

$ref = array_merge((array)$paramString, $whereArray, $orderByArray);
call_user_func_array(array($stmt, 'bind_param'), makeValuesReferenced($ref));

function makeValuesReferenced(&$arr){ 
    $refs = array(); 
    foreach($arr as $key => $value) 
        $refs[$key] = &$arr[$key]; 
    return $refs; 

}

我还没有测试过,所以不确定它是否有效。 我得到了解决方案 Pass by reference problem with PHP 5.3.1

【讨论】:

  • 嗨,Kris,我已经测试了你礼貌地给我的代码,当我用你给我的代码替换代码时,它给了我一个致命错误:Fatal error: Call to undefined function makeValuesReferenced() in ... on line 87
  • 是的,我是否复制了您答案中最后一个代码块中的代码并将其放入我的代码中。您是要我将 call_user_func_array 放入 $stmt-&gt;bind_param($paramString, ); 还是将 $stmt-&gt;bind_param($paramString, ); 完全替换为 call_user_func_array?
  • 完全替换它。删除整行 $stmt->bind_param
  • 当您将函数复制到代码中时,我不知道它怎么可能是未定义的函数。您复制了整个函数 makeValuesReferenced... 对吗?请用新代码更新您的问题
  • 是的,我将在问题中发布更新的代码供您查看。很奇怪
【解决方案2】:

同时查看您的代码,您有一些逻辑错误。

1)

$searchquestion = $questioncontent;

应该是

$searchquestion = $_GET['questioncontent'];

2)

if (empty($questioncontent)){

应该是

if (empty($searchquestion)){

3)

<td class='questiontd'>{$dbQuestionContent['QuestionContent']}</td>

应该是

<td class='questiontd'>$dbQuestionContent</td>

【讨论】:

    猜你喜欢
    • 2021-05-16
    • 2011-03-23
    • 1970-01-01
    • 1970-01-01
    • 2014-03-13
    • 2014-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多