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));
Mike Causer
  • 8,196
  • 2
  • 43
  • 63
sebbl.sche
  • 321
  • 1
  • 4
  • 20
  • 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()`](http://php.net/mysql_query) or in this [reference question.](http://stackoverflow.com/questions/6198104/reference-what-is-a-perfect-code-sample-using-the-mysql-extension) – Pekka Feb 28 '12 at 12:24
  • `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
  • Encapsulate that old mysql code to use an easier interface to it and replace it with mysqli/pdo more easily then: [PHP file cannot enter some part of code](http://stackoverflow.com/a/11580420/367456) and [How to successfully rewrite old mysql-php code with deprecated mysql_* functions?](http://stackoverflow.com/q/10919277/367456). – hakre Aug 24 '12 at 13:16
  • Braces are not needed after a while loop to execute code. [See example](http://viperpad.com/fRgdKe) – youngcouple10 Feb 28 '12 at 12:25

2 Answers2

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'];
}
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
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'];
}
miqbal
  • 2,213
  • 3
  • 27
  • 35