Saturday, 15 March 2014

PHP MySQLI Result num_rows returns null -



PHP MySQLI Result num_rows returns null -

why num_rows ever homecoming null? should homecoming number representing exact number of rows in result set.

the next code returns null num_rows:

<?php include_once 'includes/psl-config.php'; // host, user, password, database $db = new pdo('mysql:host='.host.';dbname='.database.';charset=utf8', user, password, array(pdo::attr_emulate_prepares => false, pdo::attr_errmode => pdo::errmode_exception)); if ($res = $db->query("select 'hello world!'")){ if (is_null($res)){ echo "result null<br>"; } if (is_null($res->num_rows)){ echo "num rows null<br>"; } else { echo "rows returned: ".$res->num_rows."<br>"; } while ($row = $res->fetch_row){ echo "text returned = $row[0]<br>"; } echo "done"; } else { echo "sql error"; }

the resulting page displays "num_rows null".

this simple, must missing something!

the manual says num_rows returns int. i utilize mysqli query, stores results default. don't need utilize store_result. result not null.

in pdo should not rely on rowcount() stated in php manual regarding select statements:

if lastly sql statement executed associated pdostatement select statement, databases may homecoming number of rows returned statement. however, behaviour not guaranteed databases , should not relied on portable applications.

you need utilize count(*) sql statement:

$db->query("select count(*) table_name");

php mysqli result

No comments:

Post a Comment