开发者

Accessing database and display registered users through PHP

I have a members.php page/file in which i want logged in users to see the other users registered in the database. I'm not exactly quite sure how to do this and I'm struggling quite a while now with this so your help will be VERY much appreciated.

I will include connect.php, members.php and my database.sql

Here is my connect.php file

<?php 
session_start();
//connect.php

$server = 'localhost';
$username = 'root';
$password = '';
$database = 'mydatabase';

if(!mysql_connect('localhost', 'root', ''))
{
exit('Error: could not establish database connection');
}
if(!mysql_select_db($database))
{
exit('Error: could not select the database');
}
?>

Here is my members.php file

<!DOCTYPE HTML>
<head>
<title> ShareLink </title>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body>
<div id="wrapper">
    <?php

    //create_cat.php
    include 'connect.php';
    include 'header.php';

    if(isset($_SESSION['signed_in']) == FALSE || isset($_SESSION['user_level']) != 1 )
        {
            //the user IS NOT an admin
            echo '<br/>';
            echo 'Sorry! You have to be <a href="/signin.php"><b>logged in</b></a> to view all the <a href="signup.php" title="Become a registered user!"><b>registered</b></a> members.';
            echo '<br/><br/>';
        }
        ELSE
        {
            echo '<h2>Registered users:</h2>';

            $sql = "SELECT user_name FROM users";

            //NOW I WANT TO DISPLAY ALL THE REGISTERED USERS

            //MUST I ECHO SOMETHING HERE?

            /////////////////////////////////////////////////////////
            ///////////////  WHAT CODE MUST COME HERE? //////////////
            /////////////// AND IS IT SQL OR PHP CODE? //////////////
            /////////////////////////////////////////////////////////
        }
        include 'footer.php';
    ?>
</div>

Here is my database.sql file

CREATE TABLE users (  
user_id     INT(8) NOT NULL AUTO_INCREMENT,  
user_name   VARCHAR(30) NOT NULL,  
user_pass   VARCHAR(255) NOT NULL,  
user_email  VARCHAR(255) NOT NULL,  
user_date   DATETIME NOT NULL,  
user_level  INT(8) NOT NULL,  
UNIQUE INDEX user_name_unique (user_name),  
PRIMARY KEY (user_id)  
);

CREATE TABLE categories (  
cat_id          INT(8) NOT NULL AUTO_INCREMENT,  
cat_name        VARCHAR(255) NOT NULL,  
cat_description     VARCHAR(255) NOT NULL,  
UNIQUE INDEX cat_name_unique (cat_name),  
PRIMARY KEY (cat_id)    
);

CREATE TABLE topics (  
topic_id        INT(开发者_如何学C8) NOT NULL AUTO_INCREMENT,  
topic_subject       VARCHAR(255) NOT NULL,  
topic_date      DATETIME NOT NULL,  
topic_cat       INT(8) NOT NULL,  
topic_by        INT(8) NOT NULL,  
PRIMARY KEY (topic_id)  
); 

CREATE TABLE posts (  
post_id         INT(8) NOT NULL AUTO_INCREMENT,  
post_content        TEXT NOT NULL,  
post_date       DATETIME NOT NULL,  
post_topic      INT(8) NOT NULL,  
post_by     INT(8) NOT NULL,  
PRIMARY KEY (post_id)  
);


echo '<h2>Registered users:</h2>';

$sql    = "SELECT `user_name` 
           FROM `users` 
           ORDER BY `user_name` ASC";

$result = mysql_query($sql);

while($user = mysql_fetch_array($result))
    echo $user['user_name'].'<br/>';

First you need to execute the query (mysql_query) and store it in a result. Then you have to fetch the result as an array with mysql_fetch_array within a while loop and echo the user_name of each user.

EDIT: As Damien Pirsy pointed out in his comment, it seems like user_level needs to be equal to 1 for a user to be considered as registered. If that's the case change $sql to this:

$sql    = "SELECT `user_name` 
           FROM `users` 
           WHERE `user_level` = 1 
           ORDER BY `user_name` ASC";

For your add to friend question, you could do something like this:

while($user = mysql_fetch_array($result))
    echo $user['user_name'].' 
         <a href="addfriend.php?user='.$user['id'].'">ADD TO FRIENDS</a><br/>';

And then on addfriend.php you would have some code that uses $_GET['id'] to add this person to friends.


At its most basic:

<?php
echo '<h2>Registered users:</h2>';

$sql = "SELECT user_name FROM users";

// run the query. Will return a resource or false
$result = mysql_query($sql); 

// if it ran OK
if ($result) { 
    // while I have more results, loop through them
    // returning each result as an array
    while ($user = mysql_fetch_array($result)) { 
        // use the array keys (column names) to output the data
        echo $user['user_name'], '<br />';
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜