views:

176

answers:

4

Hi, I have a query which is designed to retireve the "name" field for all records in my "tiles" table but when I use print_r on the result all I get is the first record in the database. Below is the code that I have used.

$query = mysql_query("SELECT name FROM tiles");
$tiles = mysql_fetch_array($query);

I really cant see what I have done wrong, I have also tried multiple searches within google but I cant find anything useful on the matter at hand.

+4  A: 
<?php
// Make a MySQL Connection
$query = "SELECT * FROM example"; 

$result = mysql_query($query) or die(mysql_error());


while($row = mysql_fetch_array($result)){
    echo $row['name']. " - ". $row['age'];
    echo "<br />";
}
?>
Funky Dude
Thanks, works perfectly now.
Stanni
A: 

I believe you need to do a loop to invoke fetch array until it has retrieved all the rows.

while ($row = mysql_fetch_array($query) ) {
    print_r( $row );
}
meder
+1  A: 


'mysql_fetch_array'

Returns an array that corresponds to the fetched row and moves the internal data pointer ahead.

This means that it returns array (contains values of each field) of A ROW (a record).

If you want other row, you call it again.

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
    // Do something with $row
}

Hope this helps. :D

NawaMan
+1  A: 

Use "mysql_fetch_assoc" instead of "mysql_fetch_array".

$query = mysql_query('SELECT * FROM example');

while($row = mysql_fetch_assoc($query)) :
    echo $row['whatever'] . "<br />";
endwhile;
Scott