Unix Technical Forum

PHP mysqli problem

This is a discussion on PHP mysqli problem within the MySQL General forum forums, part of the MySQL category; --> I'm having a very weird problem with $result->fetch_assoc(). Here are two very simplified examples. One uses a prepared statement ...


Go Back   Unix Technical Forum > Database Server Software > MySQL > MySQL General forum

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 06:53 AM
Jerry Schwartz
 
Posts: n/a
Default PHP mysqli problem

I'm having a very weird problem with $result->fetch_assoc(). Here are two
very simplified examples. One uses a prepared statement (although for this
exercise I'm not using any bound parameters, my results aren't affected);
the other example uses $db->query(). Look at the retrieved $row.

<?php
$db = new mysqli('host.giiexpress.com', 'giiexpr', 'xxxx', 'giiexpr_db')
or die('connect failed');
$select = 'SELECT prod_num, prod_title FROM prod WHERE prod_num = 53851 and
prod_title != "snort"';
$s = $db->prepare($select)
or die($db->error);
$s->execute()
or die($db->error);
$result = $db->store_result()
or die($db->error);
echo '$result: ' . "\n"; var_dump($result);
$num_rows = $result->num_rows;
echo '$num_rows: ' . "\n"; var_dump($num_rows);
$row = $result->fetch_assoc()
or die($db->error);
echo '$row: ' . "\n"; var_dump($row);
?>

This one gives me empty strings:

$result:
object(mysqli_result)#3 (0) {
}
$num_rows:
int(1)
$row:
array(2) {
["prod_num"]=>
string(0) ""
["prod_title"]=>
string(0) ""
}

If I simply use $db->query(), I get the proper results:

<?php
$db = new mysqli('host.giiexpress.com', 'giiexpr', 'xxxx', 'giiexpr_db
or die('connect failed');
$select = 'SELECT prod_num, prod_title FROM prod WHERE prod_num = 53851 and
prod_title != "snort"';

$result = $db->query($select);

echo '$result: ' . "\n"; var_dump($result);
$num_rows = $result->num_rows;
echo '$num_rows: ' . "\n"; var_dump($num_rows);
$row = mysqli_fetch_assoc($result);
echo '$row: ' . "\n"; var_dump($row);
?>

$result:
object(mysqli_result)#2 (0) {
}
$num_rows:
int(1)
$row:
array(2) {
["prod_num"]=>
string(5) "53851"
["prod_title"]=>
string(41) "2007 U.S. Consumer Digital Imaging Survey"
}

I've tried this on Windows using PHP 5.2.2, and on CentOS using 5.0.4. I get
the same results.

Regards,

Jerry Schwartz
The Infoshop by Global Information Incorporated
195 Farmington Ave.
Farmington, CT 06032

860.674.8796 / FAX: 860.674.8341

www.the-infoshop.com <http://www.the-infoshop.com/>
www.giiexpress.com <http://www.giiexpress.com/>
www.etudes-marche.com



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 06:54 AM
Jerry Schwartz
 
Posts: n/a
Default RE: PHP mysqli problem

I think I've figured things out. First off, I missed $db->stmt_init()
completely. Second, I missed the fact that you HAVE to bind the retrieved
values to variables, you can't create a result object and then use
$result->fetch_array(). And so on...

In short, I guess I was mixing incompatible techniques. I really need to pay
attention to the classes of all of the objects when I code. This is a
perfect example of how someone who is confused can write confused code.

I really hate the idea of binding so many parameters and so many result
variables, but I guess it is the tedious part of using prepared statements.

Regards,

Jerry Schwartz
The Infoshop by Global Information Incorporated
195 Farmington Ave.
Farmington, CT 06032

860.674.8796 / FAX: 860.674.8341

www.the-infoshop.com
www.giiexpress.com
www.etudes-marche.com


> -----Original Message-----
> From: Jerry Schwartz [mailto:jschwartz@the-infoshop.com]
> Sent: Thursday, July 26, 2007 10:29 AM
> To: mysql@lists.mysql.com
> Subject: PHP mysqli problem
>
> I'm having a very weird problem with $result->fetch_assoc().
> Here are two
> very simplified examples. One uses a prepared statement
> (although for this
> exercise I'm not using any bound parameters, my results
> aren't affected);
> the other example uses $db->query(). Look at the retrieved $row.
>
> <?php
> $db = new mysqli('host.giiexpress.com', 'giiexpr', 'xxxx',
> 'giiexpr_db')
> or die('connect failed');
> $select = 'SELECT prod_num, prod_title FROM prod WHERE
> prod_num = 53851 and
> prod_title != "snort"';
> $s = $db->prepare($select)
> or die($db->error);
> $s->execute()
> or die($db->error);
> $result = $db->store_result()
> or die($db->error);
> echo '$result: ' . "\n"; var_dump($result);
> $num_rows = $result->num_rows;
> echo '$num_rows: ' . "\n"; var_dump($num_rows);
> $row = $result->fetch_assoc()
> or die($db->error);
> echo '$row: ' . "\n"; var_dump($row);
> ?>
>
> This one gives me empty strings:
>
> $result:
> object(mysqli_result)#3 (0) {
> }
> $num_rows:
> int(1)
> $row:
> array(2) {
> ["prod_num"]=>
> string(0) ""
> ["prod_title"]=>
> string(0) ""
> }
>
> If I simply use $db->query(), I get the proper results:
>
> <?php
> $db = new mysqli('host.giiexpress.com', 'giiexpr', 'xxxx', 'giiexpr_db
> or die('connect failed');
> $select = 'SELECT prod_num, prod_title FROM prod WHERE
> prod_num = 53851 and
> prod_title != "snort"';
>
> $result = $db->query($select);
>
> echo '$result: ' . "\n"; var_dump($result);
> $num_rows = $result->num_rows;
> echo '$num_rows: ' . "\n"; var_dump($num_rows);
> $row = mysqli_fetch_assoc($result);
> echo '$row: ' . "\n"; var_dump($row);
> ?>
>
> $result:
> object(mysqli_result)#2 (0) {
> }
> $num_rows:
> int(1)
> $row:
> array(2) {
> ["prod_num"]=>
> string(5) "53851"
> ["prod_title"]=>
> string(41) "2007 U.S. Consumer Digital Imaging Survey"
> }
>
> I've tried this on Windows using PHP 5.2.2, and on CentOS
> using 5.0.4. I get
> the same results.
>
> Regards,
>
> Jerry Schwartz
> The Infoshop by Global Information Incorporated
> 195 Farmington Ave.
> Farmington, CT 06032
>
> 860.674.8796 / FAX: 860.674.8341
>
> www.the-infoshop.com <http://www.the-infoshop.com/>
> www.giiexpress.com <http://www.giiexpress.com/>
> www.etudes-marche.com
>
>
>
>
> --
> MySQL General Mailing List
> For list archives: http://lists.mysql.com/mysql
> To unsubscribe:
> http://lists.mysql.com/mysql?unsub=j...e-infoshop.com
>
>




Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 05:38 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com