1

I am using a mysql_query and mysql_fetch_array in order to return an array of values to use in a foreach loop.

For some reason only the last inputted row is returned by the query when I print_r the array that is returned.

What I am doing is:

$query = mysql_query("SELECT * from table_name;");
$returned_array = mysql_fetch_array($query);
print_r($returned_array)

Say for example there are 4 values in the table (a, b, c, d), only the last would be returned to me, so what I see is: Array ( [0] => d ).

How do I go about fixing this or am I doing something wrong?

6
  • 1
    What you should see is an E_DEPRECATED notice: please don't use mysql_*, the extension has been deprecated years ago. Learn to use PDO or mysqli_* instead (the replacement extensions) Commented Mar 23, 2015 at 10:11
  • 1
    As @EliasVanOotegem said mysql_* are deprecated and your code had some typo $returned_array - mysql_fetch_array($query); instead of = you are using - Commented Mar 23, 2015 at 10:14
  • 2
    normally you'd put it in a while loop and it'll fetch rows until it runs out Commented Mar 23, 2015 at 10:15
  • 1
    You don't have an ORDER BY clause so you cannot assume any order. Since you only fetch one row, it can be any arbitrary row—not even a random row! Commented Mar 23, 2015 at 10:16
  • 1
    When I tried to use mysqli it came up with an error such as follows (stripped due to work):Warning: mysqli_query() expects parameter 1 to be mysqli, string given in /file Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, integer given in /file Commented Mar 23, 2015 at 10:18

3 Answers 3

0

if you want to fetch all the record

$query = mysql_query("SELECT * from table_name");
while($returned_array = mysql_fetch_array($query)){
/*.......*/
}
Sign up to request clarification or add additional context in comments.

5 Comments

if it worked please mark as right so that other can get answer without difficulty
good, but what this ; means in ("SELECT * from table_name;");
pls be fair, since all users will get this as his/her references in the future.
@vivek It's not "wrong" to put a semicolon at the end of the SQL statement, it's just not how most people do it.
Also mysql_* is deprecated? Isn't it?
0

You have to use a while loop. fetch_array returns only one row.

$query = mysql_query("SELECT * from table_name;");
while( $returned_array = mysql_fetch_array($query)){
print_r($returned_array); }

Btw use mysqli. And as Álvaro G. Vicario told use an ORDER BY clause.

Typical mysqli example:

$link = mysqli_connect("localhost", "my_user", "my_password", "world");

$query = mysqli_query($link,"SELECT * from table_name;");
while( $returned_array = mysqli_fetch_array($query)){
print_r($returned_array); }

2 Comments

Cheers, that's perfect!
@swiftie821 check this mysqli example
0

Or you can use mysql_fetch_assoc() function to fetch a result row as an associative array.

$query = mysql_query("SELECT * from table_name");
while($returned_array = mysql_fetch_assoc($query)){
     //use your data for each row here like this: echo $returned_array['ID'];
}

Comments

Your Answer

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