php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42619 Problem with prepared statement
Submitted: 2007-09-10 20:50 UTC Modified: 2007-09-12 08:44 UTC
From: lmanseau at claurendeau dot qc dot ca Assigned:
Status: Not a bug Package: MySQLi related
PHP Version: 5.2.4 OS: Linux
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: lmanseau at claurendeau dot qc dot ca
New email:
PHP Version: OS:

 

 [2007-09-10 20:50 UTC] lmanseau at claurendeau dot qc dot ca
Description:
------------
I try to execute a prepared statement with mysqli. I use PHP 5.2.0 with MySQL 5.0.27

After some tests, prepared statements with MySQL instructions  such as (INSERT, DELETE and UPDATE) work very well.

The only case problem which I pointed out, it is the instruction SELECT that does not seem to react of good manner

-	SELECT * FROM table1 ? don?t work
-	SELECT C1, C2 FROM table1 - don?t work

I have no error message!!!

Reproduce code:
---------------
$db = new mysqli($hote, $util, $mpas, $base);  
if (!$db) { 
   echo "No connection with BD"; 
   exit(); 
} 

//WORK WELL 
echo 'Non prepared statement<br />'; 
$sql = "select * from ch9books";  
$rqp = $db->query($sql);  
echo 'books = '.$rqp->num_rows.'<br />'; 


//DON?T WORK
echo '<br />Prepared statement<br />'; 
$sql = "select * from ch9books";  
$rqp = $db->prepare($sql); 
$rqp->execute(); 
echo 'books = '.$rqp->num_rows.'<br />'; 

$rqp->close();


Expected result:
----------------
Non prepared statement
Books = 6 



Actual result:
--------------
Prepared statement 
Books = 0


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-09-11 08:52 UTC] uwendel at mysql dot com
Please provide a reproducible test case, including SQL definitions and "the other statements" you mention, if they are needed for a minimum test case.

Thanks!
 [2007-09-11 15:10 UTC] lmanseau at claurendeau dot qc dot ca
Here is the SQL definition:

CREATE TABLE test1 (
  c1 int(4) unsigned zerofill NOT NULL auto_increment,
  c2 varchar(10) default NULL,
  c3 double(4,2) default NULL,
  PRIMARY KEY  (c1)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

Code with problem:

// Opening connection
include ('../mysql/param.inc');
$db = new mysqli($hote, $util, $mpas, $base); 
if (!$db) {
	echo "No connection!";
	exit();
}
echo 'Non Prepared Request<br />';  //WORK WELL
$sql = "select * from test1"; 
$rqp = $db->query($sql); 
echo 'Rows = '.$rqp->num_rows.'<br />';

echo '<br />Prepared Request<br />'; //DON'T WORK
$sql = "select * from ch9books"; 
$rqp = $db->prepare($sql);
$rqp->execute();
echo 'Rows = '.$rqp->num_rows.'<br />';

// Closing connection 
$db->close();


And the results...


Non Prepared Request
Rows = 6

Prepared Request
Rows = 0
 [2007-09-12 08:39 UTC] uwendel at mysql dot com
Stupid me, ... no bug, of course - wrong API usage. 

Prepared Statements work with unbuffered result sets by default. PS can't tell you how many rows there are in the result set before you either have:

 - stored the result set / turned it into a buffered result set
 - fetched all data

You have neither called mysqli_stmt_store_result() nor fetched all results before you call mysqli_stmt_num_rows(). Therefore, mysqli_stmt_num_rows() returns 0, which is correct.

http://www.php.net/manual/en/function.mysqli-stmt-num-rows.php

"Returns the number of rows in the result set. The use of mysqli_stmt_num_rows()  depends on whether or not you used mysqli_stmt_store_result() to buffer the entire result set in the statement handle.

If you use mysqli_stmt_store_result(), mysqli_stmt_num_rows() may be called immediately. "

See also http://dev.mysql.com/doc/refman/5.1/en/mysql-stmt-num-rows.html
 [2007-09-12 08:44 UTC] uw@php.net
Wrong usage of API, ext/mysqli acts as described in the manual
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 19:01:30 2024 UTC