i am new in terms of php or in programming i know only few in php. I have a question and it probably simple for you guys, i am having a problem here to show data from mysql database, i have this code:
<?php
$getfriend= mysql_query("SELECT * from members");
$friend=mysql_fetch_array($getfriend);
$rowfriend=mysql_num_rows($getfriend);
//$users = array()--->i think i have to put it into a array, but i don't know how
if($rowfriend>1){
for ($x=0; $x < 10; $x++){
echo '<tr><td name =""><img src="" alt="" width="50" height="50" /> </td></tr>';
}
}
?>
what i want is to show pictures, names, gender, bday and so on... which is all from mysql database. i used here the so called for loop just to control how many rows i am going to show in page. my code is working it shows 3 with the problem here is i don't know how to put its content to the td's. can guys help me doing this?
How about you try something like this:-
$connection = mysql_connect('localhost', 'root', ''); //The Blank string is the password for your db
mysql_select_db('your_db_name');
$query = "SELECT * from members";
$result = mysql_query($query);
echo "<table>"; // start a table tag in the HTML
while($row = mysql_fetch_array($result)){ //Creates a loop to loop through results
echo "<tr><td>" . $row['name'] . "</td><td>" . $row['age'] . "</td></tr>"; //$row['index'] the index here is a field name
}
echo "</table>"; //Close the table in HTML
mysql_close();
Here i am using a while loop to iterate over the rows fetched and i find this the easiest way to do so.
I would also recommend switching over to mysqli and PDO (if you're determined to learn new concepts) since mysql has been depreciated.
EDIT: Asked how to limit records in db
Just change the way you write your Select queries to something like this:-
Query SELECT * FROM table LIMIT 0,5 //will return 5 records starting from the first record.
Query SELECT * FROM table LIMIT 5 //will also give the same result as above query.
If in that table there are fewer than 5 records then it will not fail but return whatever records are there.
Query SELECT * FROM table LIMIT 6,5 //will return record 7,8,9,10,11 as the index starts from 0.
Does this give you a fair idea?