1

I have one problem regarding retriving data from database. I have created one database in userrecord and its table name is tbl_user. I am trying to display the user information when they log in the system. i.e view logged user profile. The code is as follows:

<?php
    include("auth_user.inc.php"); // authrization page
    $uname=$_SESSION['user']; // logged user name
    $connection=mysql_connect("localhost","root"," ");
    mysql_select_db("userrecord",$connection);
    $sql=("select * from table_user where uname ='$uname'");
    $result=@mysql_query($sql,$connection);
    $row=mysql_fetch_array($result);
?>
<html>
    <head>
        <title>User Account</title>
    </head>
    <body bgcolor="#33CCFF">
        <br>Your Details Information Is Shown Below:<br><br>
        First Name:<?php echo $row['fname'];?><br>
        Last Name:<?php echo $row['lname'];?><br>
        Address:<?php echo $row['address'];?><br>
        E-Mail:<?php echo $row['email'];?><br>
        Gender:<?php echo $row['fname'];?><br>
        User Name:<?php echo $row['uname'];?><br>
    </body>
</html>

The code echo $row[' '] is not displaying the record from database.

5
  • If you echo $uname does it display the expected restults? Commented Sep 16, 2011 at 4:47
  • Try var_dump($row) and see what is output. This will tell you if you are getting a result in your array. If you are getting results, you'll see how the data is returned and know how to access it. Commented Sep 16, 2011 at 4:48
  • also putting @ before mysql_query means if there was any error with your query, you wouldnt see it, remove the @, if only for a while to see if you see any errors Commented Sep 16, 2011 at 4:49
  • try to run this query manually in your MySQL...then see what result it will retrieve..Thx Commented Sep 16, 2011 at 4:51
  • If you're just learning, you should also abandon the php mysql library for its more secure mysqli and PDO alternatives. Commented Sep 16, 2011 at 4:53

3 Answers 3

1

try:

<?php
    include("auth_user.inc.php"); // authrization page
    $uname = $_SESSION['user']; // logged user name
    $connection = mysql_connect("localhost","root"," ") or die('Could not connect to database: '.mysql_error());
    mysql_select_db("userrecord",$connection) or die('Could select database: '.mysql_error());
    $sql = "select * from table_user where uname ='$uname'";
    $result = mysql_query($sql) or die ('Mysql error: '.mysql_error.' - '.mysql_errno());
    $row = mysql_fetch_array($result);
?>
Sign up to request clarification or add additional context in comments.

Comments

1

and its table name is tbl_user

And your SQL was table_user?

Anyway, I didn't see others mistake with your code, just that. If it was a typo, try to debug your connection, and give us the error message...

Comments

0

no need for this @ symbol:

$result=@mysql_query($sql,$connection);

Correction:

$result=mysql_query($sql,$connection);

3 Comments

that's for error halt, he could use: $result=mysql_query($sql,$connection) or die('Mysql Connection');
This doesn't help one bit, because all you've done is remove error suppression.
if the reason their code wont work is because of a sql error, then removing the error suppression will help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.