Return multiple row without the use of mysqlnd

こ雲淡風輕ζ 提交于 2019-12-24 11:35:42

问题


I dont have mysqlnd available, so i implemented a helper function to do the job:

public function bind_array($stmt, &$row) {

    $md = $stmt->result_metadata();

        var_dump($md);
    $params = array();

    while($field = $md->fetch_field()) 
    {
        $params[] = &$row[$field->name];
    }
    call_user_func_array(array($stmt, 'bind_result'), $params);
}

The trouble is, that i also need it to be able to return multiple rows. As of now it can only return a single row.

Thanks....

EDIT: What im aiming for is getting the same result as when calling mysqli fetch_assoc, which is not available for me, as i dont have mysqlnd... The array will afterwards be json_encoded ..


回答1:


in fact, PDO is WAY easier than mysqli with mysqlnd.

You can try my wrapper for PDO which can make it even better than original class - it's incredible simple in use, combining simplicity of old mysql functions with power and safety of prepared statements.

Say, to get your multiple results you wull need one single line of code, opposite to several screens in case of mysqli or half-dozen when using old mysql functions:

$sql  = 'SELECT return, fields  FROM table WHERE search_field = ?';
$data = DB:prepare($sql)->execute([$search_val])->fetchAll();

now $data contains the desired result you can iterate over

foreach ($data as $row)
{
     echo $row['fields'];
}

Just define four constants with your DB credentials somewhere and then you'll be able to use database anywhere in your code, just like with old mysql ext used to be.



来源:https://stackoverflow.com/questions/22545979/return-multiple-row-without-the-use-of-mysqlnd

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