MySQLi fetch_row() returns null

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I am learning PHP5+MySQL. My problem is that although query() of mysqli
returns valid result, fetch_row() returns null.
Here is the code:

region_name ASC;");
while ($row = @$result->fetch_row())    // HERE PROBLEM
        $regions[$row['region_id']] = $row['region_name'];

Although $db is valid open database and $result is valid object,
$result->fetch_row() returns null.
I tested SELECT of my query() in MySQL Query Broweser but it is OK, I mean
Query Browser returns valid nonempty resultset.

Here is a function opening database:

function openRozgloszeniaDB()
        global $db, $websiteRoot;
        $password = file_get_contents("$websiteRoot/secure/.password_mysql.txt");
        $db = new mysqli("localhost", "rozgloszeniowiec",
$password, "rozgloszenia");
        if (mysqli_connect_errno())
                $db = null;
                return false;
        return true;

Could you help me please to solve the problem?
Thank you.

Re: MySQLi fetch_row() returns null

Jivanmukta escribió:
Quoted text here. Click to load it

Are you sure it's a valid object? Where do you check it? You instruct
PHP to hide all error messages and you never check return values. If
your query failed, $result will be a boolean FALSE (and, by the way,
$db->error will contain a description of the error) but your code will
never tell you.

-- - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web:
-- Mi web de humor al baño María:

Re: MySQLi fetch_row() returns null

Jivanmukta wrote:
Quoted text here. Click to load it

As Álvaro says, check return values - and DON'T BLOCK MESSAGES!

The '@' on your function calls should almost NEVER be there.  You want
the messages on your development system, and there are better ways to
disable them on your production system (i.e. display_errors=off in your
php.ini file).

Also, you should be checking the result of your $db->query() call, to
ensure it did work.

Also, don't use globals in your openRozgloszeniaDB() function (globals
make for very difficult troubleshooting and make maintenance harder -
avoid them!).  Rather, pass your website root as a parameter (or, better
yet, just use ($SERVER['DOCUMENT_ROOT']) - then you don't need to pass
anything to the function.  And return $db or false.

Finally, if you check all of this out, it is entirely possible the query
worked fine but returned no rows.  In this case, $result will be
non-false, but the first call to fetch_row() will return false.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Site Timeline