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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: jochen dot wiedmann at gmail dot com
New email:
PHP Version: OS:

 

 [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: Sun May 26 08:01:31 2024 UTC