1

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
1
  • 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 like while ($row = mysql_fetch_array($auth_data)) do_something_with($row);. Commented Apr 11, 2014 at 13:11

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0
 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.