Jump to content

Archived

This topic is now archived and is closed to further replies.

PigsHidePies

Display contents of mysql table

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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 username

mysql_connect("--host--", "--username--", "--password--"); // Connect to the database
mysql_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 information
echo "
  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]
<?php
mysql_connect("--host--", "--username--", "--password--"); // Connect to the database
mysql_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 database

while ($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 Retrieval
http://www.phpfreaks.com/forums/index.php/topic,95443.0.html - MySQL Data Retrieval (Advanced)
EDIT: put in comments... for the heck of it

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.