【问题标题】:get_result() working on localhost, but not on live serverget_result() 在本地主机上工作,但不在实时服务器上
【发布时间】:2026-02-16 00:40:01
【问题描述】:

我正在编写一个代码,用于实时搜索用户传递的查询记录。以下是编写的代码:

if(isset($_REQUEST["term"])){
    $sql = "SELECT * FROM students WHERE rollno LIKE ?";
    if($stmt = $conn->prepare($sql)){
        $stmt->bind_param("s", $param_term);
        $param_term = $_REQUEST["term"] . '%';
        if($stmt->execute()){
            $result = $stmt->get_result(); // error on this line
            if($result->num_rows > 0){
                // Fetch result rows as an associative array
                while($row = $result->fetch_array(MYSQLI_ASSOC)){
                    $rollno = $row['rollno'];
                    $name = $row['name'];
                    $image = $row['image'];
                    echo $rollno." ".$name." ".$image;
                }
            }
        }
    }
}

代码在本地服务器上运行良好。但是相同的代码在具有相同数据库的实时服务器上不起作用。我在输入一些查询后立即收到错误Fatal error: Call to undefined method mysqli_stmt::get_result() in /home/u687417727/public_html/digiclass/panel/backend-search.php on line 25 。我已经在带有注释的代码中显示了错误。可能是什么问题呢?请帮忙。尝试从很长一段时间评估它。

【问题讨论】:

  • 你能分享你的错误日志吗
  • @qdequippe 我已经用错误日志更新了代码。
  • 脚本告诉我mysqlnd没有安装,但是phpinfo怎么可能告诉我它已经安装了?
  • 你在 $name 之前是不是少了一个点? echo $rollno." "$name." ".$image;
  • @RobMoll 是的,是输入错误。原始代码没有这样的错误。我已经改正了

标签: php mysql


【解决方案1】:

您有两个选择,要么在 php_info 中的服务器上安装 mysqlnd How to enable mysqlnd for php?

或为此编写自己的函数

 function get_result($stmt){
        $stmt->execute(); //Execute query
        $stmt->store_result(); //Store the results
        $num_rows = $stmt->num_rows; //Get the number of results
        $results = NULL;
        if($num_rows > 0){
            //Get metadata about the results
            $meta = $stmt->result_metadata();
            //Here we get all the column/field names and create the binding code
            $bind_code = "return mysqli_stmt_bind_result(\$stmt, ";
            while($_field = $meta->fetch_field()){
                $bind_code .= "\$row[\"".$_field->name."\"], ";
            }
            //Replace trailing ", " with ");"
            $bind_code = substr_replace($bind_code,");", -2);
           //Run the code, if it doesn't work return NULL
            if(!eval($bind_code)) {
                $stmt->close();
                return NULL;
            }
            //This is where we create the object and add it to our final result array
            for($i=0;$i<$num_rows;$i++){
               //Gets the row by index
                $stmt->data_seek($i);
                //Update bound variables used in $bind_code with new row values
                $stmt->fetch();
                foreach($row as $key=>$value){
                    //Create array using the column/field name as the index
                    $_result[$key] = $value;
                }
                //Cast $_result to object and append to our final results
                $results[$i] = (object)$_result;
            }
        }
        $stmt->close();
        return $results;
    }

【讨论】:

    【解决方案2】:

    我解决了这个问题。

    我也对这个问题感到沮丧,但经过巨大的挣扎和冲浪后,我找到了解决这个问题的方法。如果 get_result() 在您的 cpnael 中无法使用“mysqli”扩展名,请按照以下步骤操作:

    我只是通过在我的 Cpanel 中 DISABLED "mysqli"ENABLED "nd_mysqli" 扩展来解决这个问题。现在 get_result() 语句正常工作并解决了这个令人头疼的问题。

    【讨论】:

      最近更新 更多