Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Hy,

I'm new in php and I'm having some problems with mysql_fetch_array().

$sql = "SELECT mandant_kurz FROM pu_mandant ORDER BY mandant_kurz ASC";

$result = mysql_query($sql);

$list = mysql_fetch_array($result);

There are more than 100 entries in the database but the mysql_fetch_array() delivers only one. When I'm trying it with a while-loop it isn't working either.

Here it goes with my while-loop

$sql = "SELECT mandant_kurz FROM pu_mandant ORDER BY mandant_kurz ASC";

$result = mysql_query($sql);

while($list = mysql_fetch_array($result));
share|improve this question
 
post your while loop and maybe we can help! –  youngcouple10 Feb 28 '12 at 12:23
1  
Welcome to Stack Overflow! As a side note, you are not doing any error checking in your query. You need to do that after a mysql_query() call. Otherwise, your script will break if the query fails. How to do this is outlined in the manual on mysql_query() or in this reference question. –  Pekka 웃 Feb 28 '12 at 12:24
 
Braces are not needed after a while loop to execute code. See example –  youngcouple10 Feb 28 '12 at 12:25
 
while($list = mysql_fetch_array($result)); ?? loop ended with a semicolon that means its close after all iteration without processing. –  Nikson Kanti Paul Feb 28 '12 at 12:28
 
maybe my problem is a bit more complex. I'm generating a AJAX response in PHP and want to return a associative array. I read somewhere, that I have to use while ($list = mysql_fetch_array($result)) to create it. –  sebbl.sche Feb 28 '12 at 12:34
show 1 more comment

2 Answers

up vote 2 down vote accepted

Update:

You are not doing anything inside your loop:

while($list = mysql_fetch_array($result));

Try:

while($list = mysql_fetch_array($result){
  echo $list['mandant_kurz'];
}

Also try running your query in MySQL client to make sure it actually returns 100 rows.


You will have to use loop:

while($row = mysql_fetch_array($result)){
   echo $row['mandant_kurz'];
}
share|improve this answer
add comment

This echoes just first row.

$list = mysql_fetch_array($result);
echo $list['mandant_kurz'];

Moves pointer to first row and echoes all rows

mysql_data_seek($result,0);

while( $list = mysql_fetch_array($result) ) {
  echo $list['mandant_kurz'];
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.