prepared statement method.. confused

半腔热情 提交于 2019-12-01 01:59:59

As we have defined, that the issue was with your foreach.

What is wrong is with how you're reading it, fetch does not have associative properties so need to use the bind_result.

Here is a hack that is also suggested at the fetch manual:

public function selectUserInfo($id)
{
    $stmt = $this->con->prepare("SELECT * FROM users WHERE os_id=?");
    $stmt->bind_param('i', $id);
    if(!$stmt->execute())
    {
        trigger_error($stmt->error, E_USER_ERROR);
    }
    else
    {
        $bindVarArray = array();
        $data = array();
        $result;
        $meta = $stmt->result_metadata();
        while ($column = $meta->fetch_field())
        {
            $columnName = str_replace(' ', '_', $column->name);
            $bindVarArray[] = &$result[$columnName];
        }
        call_user_func_array(array($stmt, 'bind_result'), $bindVarArray);

        $index = 0;
        while ($stmt->fetch() != null)
        {
            foreach ($result as $k => $v)
            {
                $data[$index][$k] = $v;
            }
            $index++;
        }
        return $data;
    }
}

Then you can use your foreach to read it like this:

foreach ($data as $result)
{
    echo $result['os_fname'], ' => ', $result['os_lname'], "\n";
}

And you can always use print_r to see how your resulting array is:

print_r($data);

your od_id type in DB is string or integer? if a integer

public function selectUserInfo($id){

    $stmt = $this->con->prepare("SELECT * FROM users WHERE os_id = ?");
    $stmt->bind_param("i", $id);//use 'i' instead of 's'

    if($stmt->execute() == FALSE){
        trigger_error($stmt->error, E_USER_ERROR);
    }else{
        $data = array();
        while($row = $stmt->fetch()){
            $data[] = $row;
        }
        return $data;
    }

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!