user3481478
user3481478

Reputation: 387

How to convert a sql object into a multidimensional array?

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

Upvotes: 1

Views: 519

Answers (2)

Neeraj
Neeraj

Reputation: 197

 you selected only two fields  so output will be two parameter.

> select emailadn uniqueid from tablename. parameter $temp[0]; $temp [1]
> ..................... $temp[2] $temp[3] wrong array

Upvotes: 0

moonwave99
moonwave99

Reputation: 22817

You are using MYSQL_BOTH, so you get both numeric and string keys into $temp, which holds just the first result of your query.

Use PDO/mysqli and drop mysql_* please.

Upvotes: 1

Related Questions