douyamitong57935 2014-06-23 04:27
浏览 67
已采纳

如何使用php显示数据库中的数据?

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?

  • 写回答

1条回答 默认 最新

  • dongpa6867 2014-06-23 04:31
    关注

    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?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog