开发者

mysqli_stmt to return a result object

When querying using mysqli_stmt::prepare() and execute(), it will not return the result set. But we need to access using the mysqli_stmt:fetch() on the stmt object. There is one function in the php manual called mysqli_stmt::get_result() that will return the result set into a variable we define, but when I use it, it gives me undefined method error. 开发者_Go百科The manual say it is probably in SVC which I am not sure what.

The codes:

$conn = new mysqli(DB_HOST,DB_USERNAME,DB_PASSWORD, DB_NAME);
$stmt =  $conn->stmt_init();
$stmt->prepare("select * from users where u_id = ? ");
$stmt->bind_param('s', $param);
$stmt->execute();

Edit :

I know I can use bind_result() and fetch() function to retrieve the record like :

$stmt->bind_result($column1, $column2, $column3);

while ($stmt->fetch()) {
    echo "Column 1: $column1\n";
    echo "Column 2: $column2\n";
    echo "Column 3: $column3\n";
}

But, my objective is to get the result set (the resource type object) after I execute, something like this:

$result = $stmt->execute();

so I can use the normal mysqli::fetch_object on the result set

$conn->fetch_object($result);

Is there any way to achieve this? thanks

Update:

The get_result function is exactly what I need, but it's not working on my PHP


Well, if you do not want to use mysqli_stmt::fetch() but mysqli_result::fetch_*(), I guess it will be a little cumbersome.

http://www.joekolba.com/node/2

Alternatively, use PDO.


I have done it in the following way:

$stmt->prepare("select * from users where u_id = ? ");
$stmt->bind_param('s', $param);
$stmt->execute();

$stmt->bind_result($columns['column_name1'], $columns['column_name2'], $columns['column_name3']);

while ($stmt->fetch()) {
  $row = (object) $columns;
}

That comes quite near the FETCH_OBJECT method. You can access all the data of one row via $row->column_name1 etc. Of course you would have to update the associative columns-array, when you alter your table and change names. But it works quite well for me. Didn't want to switch to PDO - considering the fact I switched to MYSQLi just some days ago ;)


To enable the method mysqli_stmt::get_result(), you need to use the mysqlnd (MySQL Native Driver) extension: http://php.net/manual/en/book.mysqlnd.php

For some reason, this concrete method is not implemented with the more usual libmysqlclient. In my debian server (and also on my ubuntu desktop) this amounted to run:

sudo apt-get install php5-mysqlnd


You need to call bind_result to tell mysqli which variables should hold the columns. Then every time you call fetch, those variables will be automatically filled in, and you can access them like normal. See the code below (not sure if it works, ymmv).

$conn = new mysqli(DB_HOST,DB_USERNAME,DB_PASSWORD, DB_NAME);

if($stmt = $conn->prepare('select * from users where u_id = ?'))
{
    $stmt->bind_param("s", $param);
    $stmt->execute();
    $stmt->bind_result($column1, $column2, $column3);

    while ($stmt->fetch()) {
        echo "Column 1: $column1\n";
        echo "Column 2: $column2\n";
        echo "Column 3: $column3\n";
    }

    $stmt->close();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜