How to use bind_result() instead of get_result() in php

前端 未结 5 1501
攒了一身酷
攒了一身酷 2020-12-18 08:57

I\'m working on a project for uni and have been using the following code on a testing server to get all devices from a table based on a user_

5条回答
  •  遥遥无期
    2020-12-18 09:35

    in order to use bind_result() you can't use queries that SELECT *.

    instead, you must select individual column names, then bind the results in the same order. here's an example:

    $stmt = $mysqli->prepare("SELECT foo, bar, what, why FROM table_name WHERE id = ?");
    $stmt->bind_param("i", $id);
    if($stmt->execute()) {
        $stmt->bind_result($foo, $bar, $what, $why);
        if($stmt->fetch()) {
            $stmt->close();
        }else{
            //error binding result(no rows??)
        }
    }else{
        //error with query
    }
    

提交回复
热议问题