php - get_result() 在本地主机上工作,但不在实时服务器上工作

标签 php mysql

我正在编写一段代码,用于通过用户传递的查询实时搜索记录。编写的代码如下:

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 在我输入一些查询之后。我已经用注释显示了代码中的错误。可能是什么问题呢?请帮助。尝试长时间评估它。

最佳答案

您有两个选择,要么在您的服务器上的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;
    }

关于php - get_result() 在本地主机上工作,但不在实时服务器上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58393464/

相关文章:

MySQL平均二级聚合

php - 有没有办法从 php 闭包更新调用者范围变量

php while循环更新单表描述中的相同值

php - 1064错误,不确定是什么问题

MySQL 按 dayofweek 分组的行计数,包括零结果

mysql - 删除所有值比第二高值低 5 倍的记录

php - 后端的 url_for 或 link_to 与管理生成器 Symfony

PHP - 比较两个 JSON 对象的结构

mysql - MySql 索引表中固定的 preprendet 字符串是否会导致性能问题?

mysql - 如何从日期到列中每个给定行值中获取最后一行?