how to generate a page with an entire collumn from a db

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
andrei.mita
Forum Commoner
Posts: 65
Joined: Sun May 08, 2005 4:06 am
Location: Barlad/Romania

how to generate a page with an entire collumn from a db

Post by andrei.mita »

I would like to make a page for my website where people can see all the other member profile. I have a have a mysql db created where all the user's info are located. How can I generate a page where all the name of the users are listed?
Thanks,
Andrei
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

if (!isset($_GET['user_id']))
{
     $result = mysql_query("SELECT * FROM `users_table` ORDER BY `user_id`") or die(mysql_error());

     while ($row = mysql_fetch_assoc($result))
     {
          echo '<a href="?user_id='.$row['id'].'>'.$row['user_name'].'</a> <br />';
     }
}
else
{
     $result = mysql_query("SELECT * FROM `users_table` WHERE `user_id` = '".$_GET['user_id']."' LIMIT 1") or die(mysql_error());
     
     if mysql_num_rows($result) == 0)
     {
          echo 'No User Found';
     }
     else
     {
          //do whatever with the user information
          print_r($row);
     }
}


Thats about as basic as you can get. This will output all the members in your users table, if you click on their name it will display only their information. I also recommend you do a quick search on "sanitation" because your query can be altered because no filtering is being done on $_GET['user_id'].. some features that may be of use are: mysql_escape_real_string, preg_match
andrei.mita
Forum Commoner
Posts: 65
Joined: Sun May 08, 2005 4:06 am
Location: Barlad/Romania

Post by andrei.mita »

I have a little problem implementing the code. I establish the connection to the mysql server, I choos the the db and then... i post the code.
the

Code: Select all

if (!isset($_GET&#1111;'user_id'])
means that you don't have to do nothin in order for the script to run, no? You don't have to push anything, right?

Thanks,
Andrei
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

if $_GET['user_id'] does not exist then run this code
is basically what it means
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

What jcart did was create a script for you that could also another question for you.

the first if statement is used to get all the rows from the db for you.

the else statement will be used if you decide to do a search for just a particular person based on their id.

two birds with one stone and all that.
Post Reply