Page 1 of 1

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

Posted: Sat May 14, 2005 11:18 am
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

Posted: Sat May 14, 2005 11:25 am
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

Posted: Sat May 14, 2005 1:53 pm
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

Posted: Sat May 14, 2005 2:07 pm
by John Cartwright
if $_GET['user_id'] does not exist then run this code
is basically what it means

Posted: Sun May 15, 2005 3:52 am
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.