Jump to content

[SOLVED] View user details


jerry89

Recommended Posts

Hi , here's what I want to do... " when you log in to the page , as user you will be redirected to userpage.php" got that working fine -> but when u get there i m trying that the user can see their details , which are in database.

 

Here's the code i got:

 

<?php

$sql = "SELECT * FROM users ";
$result = MYSQL_QUERY($sql);
$numberOfRows = MYSQL_NUMROWS($result);
if ($numberOfRows==0) {  
?>

Sorry. No records found !!

<?php
}
else if ($numberOfRows>0) {


$firstname =MYSQL_RESULT($result,$i,"firstname");
$lastname =MYSQL_RESULT($result,$i,"lastname");
$email = MYSQL_RESULT($result,$i,"email");


}
?>
    					
                        <?php echo $firstname; ?>
                        <?php echo $lastname; ?>
                        <?php echo $email; ?>

 

When I got select all it select the 1st user aka admin.

 

I was thinking what about if i put "

 sql = "SELECT $_SESSION[username] FROM users"; 

and the rest.. But it didn't work..

 

 

any ideas ?

Link to comment
https://forums.phpfreaks.com/topic/150741-solved-view-user-details/
Share on other sites

how did it not work?  Is this your entire block of code for the page ?

 

I dont see a session_start() at the top in which case your user session would not be initialized for you to use it in the SQL query.

 

also, the SQL query should be like this i think

 

$sql = "SELECT * FROM users where name = '{$_SESSION['username']}' "; 

 

also, a quick google search tells me that MYSQL_NUMROWS is deprecated, you should use mysql_num_rows()

here's all the code I got on the page

<?php session_start(); ?>
<?php require ("../includes/connection.php"); ?>


<?php include("includes/header.php"); ?>
<table id="structure">
        	<tr>
          
                <td id="page">
                	<h2> User Page</h2>
                    	
    
    				         

<?php

$sql = "SELECT * FROM users WHERE username = '{$_SESSION['username']}' ";
$result = MYSQL_QUERY($sql);
$numberOfRows = MYSQL_NUMROWS($result);
if ($numberOfRows==0) {  
?>

Sorry. No records found !!

<?php
}
else if ($numberOfRows>0) {


$firstname =MYSQL_RESULT($result,$i,"firstname");
$lastname =MYSQL_RESULT($result,$i,"lastname");
$email = MYSQL_RESULT($result,$i,"email");


}
?>
    					
                        <?php echo $firstname; ?>
                        <?php echo $lastname; ?>
                        <?php echo $email; ?>
                     </td>
                   </tr>
                 </table>
<?php include("includes/footer.php"); ?>
<?php session_destroy(); ?>

 

Okies thats the code I got..  now it shows no records found... ?

in DB are details.. so I dont know whats wrong.. :(

 

 

echo out the contents of $sql after you assign it

manually run the query in the DB and see what you get

 

you can also (and probably should) use "or die()" at the end of each SQL command to capture errors.

 

most likely there is some error with the command so it stops executing at that point in time.  your statement "There are no records" is plain HTML, not in the PHP section so it will always be printed regardless.

 

Why not just echo that statement instead of always closing and opening PHP brackets ?

true its the " No records " is HTML, but it didint show when it has a details to show.  *wonder why*

 

Well anyway

 

Thanks for now.. I be back later , i gotta rush for my spanish lesson.

 

if anyone think of anything to make work I apreciate any replys to the post..

 

thanks

like i said there is probably an issue with your SQL statement.

 

try running the same thing manually to see if it returns the results you expect.  If it does, then can troubleshoot it further

 

the other possibility is here

I dont see $i defined anywhere else on the page.  did this section of code original reside in a loop ?

 

  $firstname =MYSQL_RESULT($result,$i,"firstname");

  $lastname =MYSQL_RESULT($result,$i,"lastname");

  $email = MYSQL_RESULT($result,$i,"email");

 

I think what you want here is something like this

 

 

<?php session_start(); ?>
<?php require ("../includes/connection.php"); ?>


<?php include("includes/header.php"); ?>
<table id="structure">
          <tr>
                <td id="page">
                   <h2> User Page</h2>
                      
<?php

$sql = "SELECT * FROM users WHERE username = '{$_SESSION['username']}' ";
$result = MYSQL_QUERY($sql);
$numberOfRows = mysql_num_rows($result);
if ($numberOfRows==0) {  
   echo "Sorry. No records found !!";
}
else  {
   $row = mysql_fetch_array($result);
   
   $firstname = $row['firstname'];
   $lastname = $row['lastname'];
   $email = $row['email'];
   
   echo $firstname;
   echo $lastname;
   echo $email;
}
?>
                     </td>
                   </tr>
                 </table>
<?php include("includes/footer.php"); ?>
<?php session_destroy(); ?>

Archived

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

×
×
  • Create New...

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.