php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #48373 Unable to combine prepared statements and fetch_row
Submitted: 2009-05-24 15:27 UTC Modified: 2009-07-03 12:49 UTC
From: jochen dot wiedmann at gmail dot com Assigned:
Status: Not a bug Package: MySQLi related
PHP Version: 5.2.9 OS: Fedora Linux 10
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
8 + 22 = ?
Subscribe to this entry?

 
 [2009-05-24 15:27 UTC] jochen dot wiedmann at gmail dot com
Description:
------------
This is a follow up to bug 45289, which has been closed due to missing feedback. I have checked the latest snapshot (php5.2-200905151830), whether the problem still persists by using the script below. (Should be easy to derive a .phpt file from it.)


Reproduce code:
---------------
<?php
  $mysqli = mysqli_init();
  $mysqli->real_connect("localhost", "root", null, 'test');
  if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
  }
  $mysqli->query("DROP TABLE IF EXISTS prep_and_fetch_row")
    or die($mysqli->error);
  $mysqli->query("CREATE TABLE prep_and_fetch_row(id BIGINT NOT NULL
PRIMARY KEY, name VARCHAR(64) NOT NULL)")
    or die($mysqli->error);
  $mysqli->query("INSERT INTO prep_and_fetch_row (id, name) VALUES (1,
'abc')")
    or die($mysqli->error);
  $stmt = $mysqli->prepare("SELECT * FROM prep_and_fetch_row WHERE
id=?")
    or die($mysqli->error);
  $id = "1";
  $stmt->bind_param("i", $id)  or die($mysqli->error);
  $stmt->execute() or die($mysqli->error);
  $result = $mysqli->use_result() or die($mysqli->error);
  while ($row = $result->fetch_row()) {
    print "----- result row -----------\n";
    print_r($row);
  }
  print "----- no more results -----------\n";

  $mysqli->query("DROP TABLE prep_and_fetch_row") or
die($mysqli->error);
  $mysqli->close();
?>


Expected result:
----------------
Expected output: Something like

----- result row -----------
Array
(
    [0] => int(1)
    [1] => string(3) "abc"
)
----- no more results -----------


Actual result:
--------------
Actual output:

----- result row -----------
Array
(
    [0] => 
    [1] => 
)
----- no more results -----------


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-06-05 20:41 UTC] jochen dot wiedmann at gmail dot com
Ping?
 [2009-07-03 12:49 UTC] uw@php.net
After removal of "or die()" for mysqli->use_result, PHP will tell you that your script is borked:

nixnutz@ulflinux:~/src/login/php5> sapi/cli/php  bar.php

Fatal error: Call to a member function fetch_row() on a non-object in /home/nixnutz/src/login/php5/bar.php on line 23

nixnutz@ulflinux:~/src/login/php5> cat bar.php
<?php
  $mysqli = mysqli_init();
  $mysqli->real_connect("localhost", "root", root, 'test');
  if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
  }
  $mysqli->query("DROP TABLE IF EXISTS prep_and_fetch_row")
    or die($mysqli->error);
  $mysqli->query("CREATE TABLE prep_and_fetch_row(id BIGINT NOT NULL
PRIMARY KEY, name VARCHAR(64) NOT NULL)")
    or die($mysqli->error);
  $mysqli->query("INSERT INTO prep_and_fetch_row (id, name) VALUES (1,
'abc')")
    or die($mysqli->error);
  $stmt = $mysqli->prepare("SELECT * FROM prep_and_fetch_row WHERE
id=?")
    or die($mysqli->error);
  $id = "1";
  $stmt->bind_param("i", $id)  or die($mysqli->error);
  $stmt->execute() or die($mysqli->error);
  $result = $mysqli->use_result();
  while ($row = $result->fetch_row()) {
    print "----- result row -----------\n";
    print_r($row);
  }
  print "----- no more results -----------\n";

  $mysqli->query("DROP TABLE prep_and_fetch_row") or
die($mysqli->error);
  $mysqli->close();
?>



... and PHP is right. With MySQL Prepared Statements you have to bind results. There is no way in PHP 5.2.x to get a resultset object from a statement.

 [2009-07-03 12:53 UTC] uw@php.net
After removal of "or die()" for mysqli->use_result, PHP will tell you that your script is borked:

nixnutz@ulflinux:~/src/login/php5> sapi/cli/php  bar.php

Fatal error: Call to a member function fetch_row() on a non-object in /home/nixnutz/src/login/php5/bar.php on line 23

nixnutz@ulflinux:~/src/login/php5> cat bar.php
<?php
  $mysqli = mysqli_init();
  $mysqli->real_connect("localhost", "root", root, 'test');
  if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
  }
  $mysqli->query("DROP TABLE IF EXISTS prep_and_fetch_row")
    or die($mysqli->error);
  $mysqli->query("CREATE TABLE prep_and_fetch_row(id BIGINT NOT NULL
PRIMARY KEY, name VARCHAR(64) NOT NULL)")
    or die($mysqli->error);
  $mysqli->query("INSERT INTO prep_and_fetch_row (id, name) VALUES (1,
'abc')")
    or die($mysqli->error);
  $stmt = $mysqli->prepare("SELECT * FROM prep_and_fetch_row WHERE
id=?")
    or die($mysqli->error);
  $id = "1";
  $stmt->bind_param("i", $id)  or die($mysqli->error);
  $stmt->execute() or die($mysqli->error);
  $result = $mysqli->use_result();
  while ($row = $result->fetch_row()) {
    print "----- result row -----------\n";
    print_r($row);
  }
  print "----- no more results -----------\n";

  $mysqli->query("DROP TABLE prep_and_fetch_row") or
die($mysqli->error);
  $mysqli->close();
?>



... and PHP is right. With MySQL Prepared Statements you have to bind results. There is no way in PHP 5.2.x to get a resultset object from a statement.

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 16:01:29 2024 UTC