PigsHidePies Posted September 14, 2006 Share Posted September 14, 2006 How does one go about displaying all rows within a table based upon a userid? Say for example, a profile page of a site member. How would I display various information collected in a table based upon a cookie? Heres some code to help clarify(it is not complete and probably wrong ):[code]<?php$username=$_SESSION['valid_user']; //setup connection@ $db = new mysqli('localhost', 'username', 'password', 'database');if (mysqli_connect_errno()){echo 'Error: Cannot connect to database.';exit;}$query = "select username, email from database";$result=$db->query($query);while($row = mysqli_fetch_assoc($result)){ echo $row['username']; echo $row['email'];}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/20690-display-contents-of-mysql-table/ Share on other sites More sharing options...
genericnumber1 Posted September 14, 2006 Share Posted September 14, 2006 I'm not totally sure what you are interested in doing. Do you mean you wish for the script to look at a cookie (like for a username) and then post all the information for that username from a mysql table? If so...[code]<?php$username = $_COOKIE['username']; // Get the usernamemysql_connect("--host--", "--username--", "--password--"); // Connect to the databasemysql_select_db("--database--"); // Select the database$query = "SELECT * FROM table_name WHERE username='$username' LIMIT 1"; // Set the query to pick only one row where the username matches the cookie username (insecure)$result = mysql_query($query); // Query the database$row = mysql_fetch_array($result, MYSQL_ASSOC); // Get the information from the $result resource and put it in the $row array// Set some variables to the $row array$email = $row['email'];$favoritecolor = $row['favcolor'];// Display the informationecho " Email: $email <br> Favorite Color: $favoritecolor";?>[/code](That will only display one row of information)To display the multiple rows... (not based on username or anything)[code]<?phpmysql_connect("--host--", "--username--", "--password--"); // Connect to the databasemysql_select_db("--database--"); // Select the database$query = "SELECT * FROM table_name"; // Set the query to select everything from the table$result = mysql_query($query); // Query the databasewhile ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { // Loop through all the rows // Set some variables to the $row array $username = $row['username']; $email = $row['email']; $favoritecolor = $row['favcolor']; // Display the information echo " Username: $username <br> Email: $email <br> Favorite Color: $favoritecolor <br><br>";}?>[/code]it's not secure though, and definately not clean...Oh yes, visit these as well...http://www.phpfreaks.com/forums/index.php/topic,95441.0.html - MySQL Data Retrievalhttp://www.phpfreaks.com/forums/index.php/topic,95443.0.html - MySQL Data Retrieval (Advanced)EDIT: put in comments... for the heck of it Quote Link to comment https://forums.phpfreaks.com/topic/20690-display-contents-of-mysql-table/#findComment-91517 Share on other sites More sharing options...
gerkintrigg Posted September 14, 2006 Share Posted September 14, 2006 Okay here goes...Let me know if I confuse you - I'm training to be an I.T. tutor, so it'd be good feedback ;)Presuming that you're posting the userid to the connecting page to get the info using (for example) getinfo.php?userid=1;To get an array (or list) of "this row has this in it" from a database you need something like this:[code]// open the database connection - i use and include for this:include 'db.php';//then the sql query:$sql=mysql_query("SELECT * FROM member WHERE userid =".$_REQUEST['userid']);// then get the array:$row=mysql_fetch_array($sql);[/code]Now the $row variable has all the database info in it that you need, so you might want to put the username on screen, just use:[code]echo $row['Username'];[/code]That wasn't too hard I hope ;)Cheerio. Quote Link to comment https://forums.phpfreaks.com/topic/20690-display-contents-of-mysql-table/#findComment-91543 Share on other sites More sharing options...
PigsHidePies Posted September 14, 2006 Author Share Posted September 14, 2006 EDIT: I got it to work using your suggestion, gerkintrigg, thanks. Genericnumber1, you said the code wasn't secure about that which you wrote easlier...could you(or someone) please explain how it isn't secure? Is it possible for someone to edit the cookie and insert malicious code? What if I used a mysqli_real_escape string on the variable $username? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/20690-display-contents-of-mysql-table/#findComment-91545 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.