Maria T
Maria T

Reputation: 333

displaying user's records from database

<?php
include "db.php"; 
$username=$_POST['username']; 
$email=$_POST['email'];


$query="SELECT * FROM members where username = '".mysql_real_escape_string($username)."'";


$result=mysql_query($query)or die(mysql_error());


$user = mysql_fetch_assoc($result);
mysql_close();
?> <br /> <p></p>

Welcome back! Your details below: <br /><br /> 

<table border="1" cellspacing="2" cellpadding="5"> 
<tr> 
 <th>First Name</th> 
 <th>Last Name</th>
 <th>User Name</th>
 <th>Email</th> 
 <th>Age</th> 
</tr>

<?php

$firstname= $user['firstname']; 
$lastname= $user['lastname']; 
$username= $user['username']; 
$email= $user['email']; 
$age= $user['age']; 
?>

 <tr> 
   <td><? echo $firstname ?></td> 
   <td><? echo $lastname ?></td>
   <td><? echo $username ?></td>
   <td><? echo $email ?></td>
   <td><? echo $age ?></td>
 </tr>
</table>

guys, i use this code to display the user's details, BUT STILL its not displaying the records.

what's wrong with this code? hmm... there's no error, but its not working.

:'(

Upvotes: 0

Views: 377

Answers (3)

Codex73
Codex73

Reputation: 5766

Try adding a loop like this below replacing the single record set with multiple. Also check your query and see if it's correct.

<?php
include "db.php"; 
$username=$_POST['username']; 
$email=$_POST['email'];


$query="SELECT * FROM members where username = '".mysql_real_escape_string($username)."'";

$result=mysql_query($query)or die(mysql_error());

//$user = mysql_fetch_assoc($result);



?> 
<br /> <p></p>

Welcome back! Your details below: <br /><br /> 

<table border="1" cellspacing="2" cellpadding="5"> 
<tr> 
 <th>First Name</th> 
 <th>Last Name</th>
 <th>User Name</th>
 <th>Email</th> 
 <th>Age</th> 
</tr>

<?php
while($user=mysql_fetch_array($result))
{
echo '<tr>';
echo '<td>'.$user['firstname'].'</td>
   <td>'.$user['lastname'].'</td>
   <td>'.$user['username'].'</td>
   <td>'.$user['email'].'</td>
   <td>'.$user['age'].'</td>';
echo '</tr>';
}
mysql_close();
?> 
</table>

Upvotes: 0

mvds
mvds

Reputation: 47034

Put error_reporting(~0) at the very top to make sure you get reports on everything going less than perfect. Do a print_r($_POST) to make very sure you get in $_POST what you think you are getting.

If you then still haven't found the problem, provide more context! (like: script output)

(btw: you should echo htmlspecialchars($user[...]); or people can put very nasty stuff in there.)

And make sure you have "short_open_tag = On" in your php.ini, or use

<?php

instead of

<?

everywhere!

Upvotes: 0

funwhilelost
funwhilelost

Reputation: 2010

Try adding an echo(mysql_error()) to see if there's a MySQL error beyond just a bad query.

Upvotes: 1

Related Questions