Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
细长php$stmt->;将_result()获取到$stmt->;bind_result()_Php_Mysqli - Fatal编程技术网

细长php$stmt->;将_result()获取到$stmt->;bind_result()

细长php$stmt->;将_result()获取到$stmt->;bind_result(),php,mysqli,Php,Mysqli,我的问题是如何将get_result()转换为bind_result() 这是我的index.php // // 下面是dbhandler.php public function getAllUserTasks($user_id) { $stmt = $this->conn->prepare("SELECT t.* FROM tasks t, user_tasks ut WHERE t.id = ut.task_id AND ut.user_id = ?"); $s

我的问题是如何将get_result()转换为bind_result()

这是我的index.php //

//

下面是dbhandler.php

 public function getAllUserTasks($user_id) {
    $stmt = $this->conn->prepare("SELECT t.* FROM tasks t, user_tasks ut WHERE t.id = ut.task_id AND ut.user_id = ?");
    $stmt->bind_param("i", $user_id);
    $stmt->execute();
    $tasks = $stmt->get_result();
    $stmt->close();
    return $tasks;
}
/**
 * Fetching all user tasks
 * @param String $user_id id of the user
 */
public function getAllUserTasks($user_id) {
    $stmt = $this->conn->prepare("SELECT t.* FROM tasks t, user_tasks ut WHERE t.id = ut.task_id AND ut.user_id = ?");
    $stmt->bind_param("i", $user_id);
    $stmt->execute();
    $stmt->bind_result($id, $task, $status, $created_at);
    $tasks = array();
    while($stmt->fetch()) {
        $tmp = array();
        $tmp["id"] = $id;
        $tmp["task"] = $task;
        $tmp["status"] = $status;
        $tmp["createdAt"] = $created_at;
        array_push($tasks, $tmp);
    }
    $stmt->close();
    return $tasks;
}
我在尝试将其转换为使用bind_result()而不是get_result()时遇到了各种各样的问题,能否请一些人给我一些建议

提前谢谢

杰森

这里是我的方法,试图转换它没有运气请指出问题,谢谢

index.php ///

dbhealper.php //

dbhealper.php//

你可以试试这个

public function getAllUserCompanies($user_id) {
        $stmt = $this->conn->prepare("SELECT CompanyID, UserID, CompanyName, CompanyAddress, CompanyCity, CompanyIndustry, CompanyContact, CompanyNotes, CreatedDate, UpdatedTime, UpdatedBy FROM company WHERE UserID = ?");
        $stmt->bind_param("i", $user_id);
        $stmt->execute();
        /* Store the result (to get properties) */
        $stmt->store_result();

        /* Get the number of rows */
        $num_of_rows = $stmt->num_rows;

        /* Bind the result to variables */
        $companies = $stmt->bind_result($CompanyID, $UserID, $CompanyName, $CompanyAddress, $CompanyCity, $CompanyIndustry, $CompanyContact, $CompanyNotes, $CreatedDate, $UpdatedTime, $UpdatedBy);
    return $companies;
        /* free results */
        $stmt->free_result();
}

如果你看,它没有做太多的更改,但是为了存储结果,我在
$stmt->execute()之后添加了一行
$stmt->store_result()

我建议不要在任何php代码中使用
get_result()
,因为它需要安装
mysqlnd
,并且在
libmysql
中不受支持,libmysql是用于从php与mysql进行接口的最常用库。相反,应该使用
fetch()
从结果集中创建行数组,或者对于只返回一行的情况,使用
bind\u result()

public function getAllUserTasks($user_id) {
    $tasks = [];
    $stmt = $this->conn->prepare("SELECT t.* FROM tasks t, user_tasks ut WHERE t.id = ut.task_id AND ut.user_id = ?");
    $stmt->bind_param("i", $user_id);
    $stmt->execute();
    if($stmt->num_rows > 0)
    {
         while($row = $stmt->fetch())
        {
            $tasks[] = $row;
        }
        $stmt->close();
    }
    return $tasks;
}
编辑

使用index.php

$app->get('/companies', 'authenticate', function() {
    global $user_id;
    $db = new DbHandler();

    // fetching all user tasks
    $response = $db->getAllUserCompanies($user_id);
    if(!empty($response))
    {
        echoRespnse(200, $response);
    }else{
        //handle your error
    }
});

如果您在$stmt->get_result中遇到错误,可能是因为以下原因: 它仅适用于mysqlnd驱动程序(……使用以下方法解决它: DbHandler.php

 public function getAllUserTasks($user_id) {
    $stmt = $this->conn->prepare("SELECT t.* FROM tasks t, user_tasks ut WHERE t.id = ut.task_id AND ut.user_id = ?");
    $stmt->bind_param("i", $user_id);
    $stmt->execute();
    $tasks = $stmt->get_result();
    $stmt->close();
    return $tasks;
}
/**
 * Fetching all user tasks
 * @param String $user_id id of the user
 */
public function getAllUserTasks($user_id) {
    $stmt = $this->conn->prepare("SELECT t.* FROM tasks t, user_tasks ut WHERE t.id = ut.task_id AND ut.user_id = ?");
    $stmt->bind_param("i", $user_id);
    $stmt->execute();
    $stmt->bind_result($id, $task, $status, $created_at);
    $tasks = array();
    while($stmt->fetch()) {
        $tmp = array();
        $tmp["id"] = $id;
        $tmp["task"] = $task;
        $tmp["status"] = $status;
        $tmp["createdAt"] = $created_at;
        array_push($tasks, $tmp);
    }
    $stmt->close();
    return $tasks;
}
在index.php中:

/**
 * Fetching all user tasks
 * @param String $user_id id of the user
 */
public function getAllUserTasks($user_id) {
    $stmt = $this->conn->prepare("SELECT t.* FROM tasks t, user_tasks ut WHERE t.id = ut.task_id AND ut.user_id = ?");
    $stmt->bind_param("i", $user_id);
    $stmt->execute();
    $stmt->bind_result($id, $task, $status, $created_at);
    $tasks = array();
    while($stmt->fetch()) {
        $tmp = array();
        $tmp["id"] = $id;
        $tmp["task"] = $task;
        $tmp["status"] = $status;
        $tmp["createdAt"] = $created_at;
        array_push($tasks, $tmp);
    }
    $stmt->close();
    return $tasks;
}

我猜代码示例来自

您是否也可以演示如何尝试为bind_result()和bind_result()编写代码不适用于使用*的SQL查询,只需使用列名,我相信它会对您起作用。\n请参见上面的感谢此dbhandler.php看起来不同的方法如果我们使用您的dbhander.php cheers,index.php会是什么样子只需替换该死的函数。简单如饼。与以前完全相同。它返回一个re数组结果与原来的一样。我遇到了一个致命错误:在index.php页面中对非对象调用成员函数fetch()时出错,如果它说
BOOL(FALSE)
则表示查询中有错误。请尝试从命令行或phpmyadmin运行它。如果它说
(NULL)
检查您的数据库连接。这仍然会导致致命错误:对Cheers中的非对象调用成员函数fetch(),我们可以尝试此
$stmt->bind\u结果($CompanyID、$UserID、$CompanyName、$CompanyAddress、$CompanyCity、$CompanyIndustry、$CompanyContact、$CompanyNotes、$CreatedDate、$updateTime、$UpdatedBy);return$stmt->fetch();
如果在datahandler.php中执行flect(),那么index.php cheers中的循环将如何工作当($db->getAllUserCompanies($user_id)){仍然没有运气,这件事让我发疯,从周五开始就一直在做,仍然找不到解决简单问题的方法。。任何其他建议都会非常有用,干杯
/**
 * Fetching all user tasks
 * @param String $user_id id of the user
 */
public function getAllUserTasks($user_id) {
    $stmt = $this->conn->prepare("SELECT t.* FROM tasks t, user_tasks ut WHERE t.id = ut.task_id AND ut.user_id = ?");
    $stmt->bind_param("i", $user_id);
    $stmt->execute();
    $stmt->bind_result($id, $task, $status, $created_at);
    $tasks = array();
    while($stmt->fetch()) {
        $tmp = array();
        $tmp["id"] = $id;
        $tmp["task"] = $task;
        $tmp["status"] = $status;
        $tmp["createdAt"] = $created_at;
        array_push($tasks, $tmp);
    }
    $stmt->close();
    return $tasks;
}