The PHP Manual says that mysql_fetch_array is deprecated and that for new development the use of the MySQLi functions are recommended.

I have elected to use the object oriented style (shown below).

I am assigning the fetch_array $Result to the $Row variable. If I put gettype($Row) inside the while loop it says that the type for $Row is array.

However, if I put gettype($Row) just below the closing bracket for the while loop it says the type for $Row is NULL.

I am using $Rows as $Key => $Row in a foreach loop and it is working fine.

I have two questions:

Question 1.Why does gettype($Row) say that the $Row variable is an array while inside the while loop; and then say that the $Row variable is NULL immediately below the closing bracket for the while loop?

Question 2.Am I using proper coding technique using the $Row variable in the foreach loop or should I be using $Value, like $Rows as $Key => $Value, in the foreach loop?

If you are doing lots of array comparisons with your results then fetching arrays is a good thing, but otherwise I learned early on (after frequently visiting nested array hell myself ) that using the object notation wherever possible is a) easier to read (for me) b) easier to use in double quotes (you dont need to concat strings and c) really gets you into the mode of thinking about OOP.

// this is less error prone, less typing, easier to scan
echo "I live in $obj->Country now.";
//vs
// than this
echo "I live in " . $row['Country'] . " now";

I hate it when people make assumptions about sample code posted by users, and then never answer the actual question. It's happened to me on every board I've ever been on.

Ok, Volitics, here's your answer.....

// EX: I want you to fetch the next row
// then do whatever is in the loop, but only
// if there WAS a next row to fetch.
// What it means is that, at some point
// fetch_array will return null because
// there are no more rows to return
while($Row = $Result -> fetch_array())
{
// this is ok, because we are inside the loop
// the only to GET inside the loop is if
// fetch_array succeeded, which means
// we have a valid row
echo gettype($Row);
}
// this one will be null because this 'Row'
// was generated on the last repitition of
// the while loop, meeting the condition
// to exit the loop
echo gettype($Row);

If you need a linear explanation...

// assume $results has 3 records...
// pointer is now on record 1
$row = get_a_row($results);
if ($row != null) { do_something_with_row($row); } // this is called
if ($row == null) { exit_out(); }
// pointer is now on record 2
$row = get_a_row($results);
if ($row != null) { do_something_with_row($row); } // this is called
if ($row == null) { exit_out(); }
// pointer is now on record 3
$row = get_a_row($results);
if ($row != null) { do_something_with_row($row); } // this is called
if ($row == null) { exit_out(); }
// pointer is now beyond the end of the recordset
$row = get_a_row($results); // returns null
if ($row != null) { do_something_with_row($row); }
if ($row == null) { exit_out(); } // this is called

I had already plumped for PDO because I found an class written for PHP4 which did a reasonable job emulating the main benefits of what would be the future PDO.

I normally have to ignore questions about mysqli because I have never used it except when boning up on the diffs between PHP4 and PHP5.

One of the main benefits of using either of these methods is to be able to use prepared statements which help to protect your database from sql injection attacks. Once you have got your head around how they work I'm sure you will find you are writing cleaner code. I frankly find PDOs prepared statements easier to use and I prefer its API.