I wrote this simple code and I extracted two columns from MySQL table. Then I couldnt convert it into a multidimensional array.
So the table I had,have only 2 rows and what I expected it will give me a output of 2 rows and 2 columns. But instead the array size came out to be 4(instead of 2) and moreover its giving an error for the printing of temp[2] and temp[3]
<?php
mysql_connect("localhost","root",""); //Connecting to the localhost
mysql_select_db("user"); //Selecting a database
$auth_data = mysql_query("SELECT EMAIL,UNIQUE_ID FROM authentication");
#Converting the object to an array
$temp = mysql_fetch_array($auth_data, MYSQL_BOTH);
echo sizeOf($temp);
echo $temp[0];
echo $temp[1];
echo $temp[2];
echo $temp[3];
?>
Error:
[email protected]
Notice: Undefined offset: 2 in C:\xampp\htdocs\test\test.php on line 11
Notice: Undefined offset: 3 in C:\xampp\htdocs\test\test.php on line 12
mysql_fetch_array()only fetches one row (its columns as an array); it then advances the pointer so that the next call will fetch the next row. You need to call it repeatedly, usually in a loop likewhile ($row = mysql_fetch_array($auth_data)) do_something_with($row);.