codeigniter 调用未定义的方法CI_DB_mysqli_result::fetch_assoc()

pw9qyyiw  于 2022-12-07  发布在  Mysql
关注(0)|答案(1)|浏览(128)

你好,我想通过点击编辑按钮更新该行,但它给我这个错误,请帮助

function updateUser()
{
    $data = stripslashes(file_get_contents("php://input"));
    $mydata = json_decode($data, true);
    $id = $mydata['sid'];
    //retrieve specific info 
    $sql = "SELECT * FROM admin WHERE id = {$id}";
    $result = $this->db->query($sql); 
    $row = $result->fetch_assoc();
    echo json_encode($row); //returning json formate
}
i5desfxk

i5desfxk1#

使用$result->row_array();代替$result->fetch_assoc();
为了防止sql注入,您可能还希望单独绑定$id,而不是将其连接到查询中,如下所示:

$sql = "SELECT * FROM admin WHERE id = ?";
$result = $this->db->query($sql, array($id));
$row = $result->row_array();

另请参阅:https://codeigniter.com/userguide3/database/queries.html#query-bindings

相关问题