3

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));
6
  • post your while loop and maybe we can help! Commented Feb 28, 2012 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. Commented Feb 28, 2012 at 12:24
  • Braces are not needed after a while loop to execute code. See example Commented Feb 28, 2012 at 12:25
  • while($list = mysql_fetch_array($result)); ?? loop ended with a semicolon that means its close after all iteration without processing. Commented Feb 28, 2012 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. Commented Feb 28, 2012 at 12:34

2 Answers 2

3

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'];
}
Sign up to request clarification or add additional context in comments.

Comments

0

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'];
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.