0

I've a select query say

 $query = "select name from class";
 $equery = mysql_query($query);
 $v = mysql_fetch_array($equery);    
 print_r($v['name']);

I've 10 records in my data base but always i'm getting only one value.is there any alternative to retrieve my column in array format.

2
  • Using mysql_query is probably a very bad idea as it's very hard to use correctly. Is there any reason you're not using mysqli or PDO? Commented Sep 27, 2012 at 15:12
  • Dont you have to loop through results to print them? Commented Sep 27, 2012 at 15:12

6 Answers 6

2

You need to loop it.

$query = "select name from class";
$equery = mysql_query($query);
$result = array();
while ($row = mysql_fetch_array($equery)) {
    $result[] = $row['name'];
}
print_r($result);    
Sign up to request clarification or add additional context in comments.

Comments

1

Do not use the mysql extension, as it has been deprecated. You should use mysqli_result::fetch_all with the mysqli extension, or PDOStatement::fetchAll with the PDO extension.

Comments

0

Put everything in a LOOP:

<?php

  while($v = mysql_fetch_array($equery);) {
  print_r($v['name']);
}
?>

Comments

0
 $query = "select name from class";
 $equery = mysql_query($query);

 $v=null;
 $index=0;
 while($row = mysql_fetch_array($equery,MYSQL_ASSOC)){
     $v[$index++]=$row;
 }    

Comments

0

In addition to the other answers which are correct: You shouldn't really be using any of the old mysql commands as they are being depreciated. You should check out mysqli or PDO.

Comments

0

You don't essentially need to loop it, if you have only one row in output, this way is fine, but if you have more, you need to loop.

$a=mysql_fetch_array($resource)

returns the first row, loop it

while($row=mysql_fetch_assoc($resource))
{
echo ($row['name']);
}

mysql_fetch_assoc returns output in form of associative array. i keep saying this on all mysql posts, try switching to PDO. USE of mysql_query is discouraged. It means it's support may soon be removed.

http://php.net/manual/en/function.mysql-query.php

1 Comment

It's not only discouraged, it's finally in the first stages of the long-awaited deprecation process :), it might even be at the stage of issuing warnings with E_STRICT | E_ALL set...

Your Answer

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