i have a question for you guys. im trying to print an array where it would display 10 values of the table acording to user. this is what i have so far and it displays only the first row,
session_start();
// Retrieve all the data from the table
$result = mysql_query("SELECT name,location,l开发者_运维技巧ogin_id FROM table WHERE login_id = $user[login_id]")
or die(mysql_error());
// store the record of the "example" table into $row
$row = mysql_fetch_array( $result );
// Print out the contents of the entry
echo " name ".$row['name'];
echo " located ".$row['location'];
..... how can i display the first 10 rows? help would be apreciated. thank you for reading.
also add "limit 10" to the query.
SELECT name,location,login_id FROM table WHERE login_id = $user[login_id] LIMIT 10
session_start();
// Retrieve all the data from the table
$result = mysql_query("SELECT name,location,login_id FROM table WHERE login_id = $user[login_id] LIMIT 10")
or die(mysql_error());
while($row = mysql_fetch_array( $result )){
echo " name ".$row['name'];
echo " located ".$row['location'];
}
And This will work only if your login_id is not unique and multiple rows can have the same login_id
You have to call mysql_fetch_array
repeatedly to get all the row from the result set:
while(($row = mysql_fetch_array( $result ))) {
echo " name ".$row['name'];
echo " located ".$row['location'];
}
See further examples in the documentation.
If you really want to get only the first 10 rows, have a look at @Yasser Souri's answer (you still have to loop though).
精彩评论