dousi6701 2013-10-07 22:27
浏览 31
已采纳

会员数据库 - 全部显示?

So I'm not the best with PHP but everyone has to start somewhere.

I am wondering how I would go about taking all of the rows from a database, taking out the data from a certain column and outputting it in a certain way.

The reason I'm doing this is to make it show up with all the users registered on one page, sort of like a members page any forums would have.

I was thinking of somehow fetching the rows than putting the data into arrays than doing a foreach statement to print it out as each variable.

I'm not the best logical thinker but I will give a bit of database information below :)

This is an image of the structure of the database.

enter image description here

To show you guys that I am actually putting an effort into this, this is what I got so far with my terrible PHP skills ;)

$result = mysql_query("SELECT * FROM users");
while($row = mysql_fetch_array($result)) { 
    $username=array($row['username']);
    $email=array($row['email']);
    $firstname=array($row['firstname']);
    $lastname=array($row['lastname']);
    $motto=array($row['motto']);
    $bio=array($row['bio']);
}
foreach ($username as &$item) {
    print $item;
}

The one reason this code wouldn't work is because than its just selecting a row of the person with the id that is equal to $userid. This means it would grab the row info, not the column info.

Not sure how to do this as I've only done stuff with rows so far in my PHP adventure, not so much with columns :(

  • 写回答

1条回答 默认 最新

  • douliang2935 2013-10-07 22:47
    关注

    The way it works is, each time you loop through using "while", you have a new set of information. Just display your stuff right there. It's way easier than you think, which is why you have commenters complaining.

    But DON'T use the mysql_* functions. Use mysqli or PDO. I know that there are tons of examples out there using mysql_*, but that's because they're either old examples or bad ones. Don't use it. I have taken an example from the PHP manual and adjusted it for you.

    $link = mysqli_connect("localhost", "my_user", "my_password", "world");
    
    /* check connection */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s
    ", mysqli_connect_error());
        exit();
    }
    
    $result = mysqli_query($link, "SELECT * FROM users ORDER BY username");
    if($result) {
        while($row = mysqli_fetch_assoc($result)) { 
            $username=$row['username'];
            $email=$row['email'];
            $firstname=$row['firstname'];
            $lastname=$row['lastname'];
            $motto=$row['motto'];
            $bio=$row['bio'];
    
            //display your row of user information here
            echo "<div>";
            echo $username;  //etc.
    
            echo "</div>";
        }
    
        mysqli_free_result($result);
    }
    mysqli_close($link);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP