hi,
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 thy logged in the syste. i.e view logged user profile. The code is as follow:

<?php
include("auth_user.inc.php"); // authrization page
$uname=$_SESSION; // 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;?><br>
Last Name:<?php echo $row;?><br>
Address:<?php echo $row;?><br>
E-Mail:<?php echo $row;?><br>
Gender:<?php echo $row;?><br>
User Name:<?php echo $row;?><br>
</body>
</html>


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

Regards,
Prabhat Kumar Jha

Remove @ before mysql_query. Use die(mysql_error()); ie., mysql_query($sql) or die (mysql_error());

I don't think I ever seen mysql_fetch_array used without some type of loop. How are you specifying which row of data you want to display in the url? This is how I would probably do it.

<?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);

?>
<html>
<head><title>User Account</title>
</head>
<body bgcolor="#33CCFF">
<br>Your Details Information Is Shown Below:<br><br>
<?
$counter = 0;
while($row = mysql_fetch_array($result) && $counter < 1)
{
        $counter++;
        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']; ?>
}
?>
</body>
</html>

I don't think I ever seen mysql_fetch_array used without some type of loop.

You can. It will fetch only the 1st record.

I don't think I ever seen mysql_fetch_array used without some type of loop. How are you specifying which row of data you want to display in the url? This is how I would probably do it.

<?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);

?>
<html>
<head><title>User Account</title>
</head>
<body bgcolor="#33CCFF">
<br>Your Details Information Is Shown Below:<br><br>
<?
$counter = 0;
while($row = mysql_fetch_array($result) && $counter < 1)
{
        $counter++;
        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']; ?>
}
?>
</body>
</html>

I have tried the code but error is :
Parse error: parse error, unexpected T_STRING in c:\phpdev5www\prabhat\user_profile.php on line 20

I.e First Name:<?php echo $row; ?> <br>
In this line <?php is not highlighted.

Please reply

1. You don't have session_start
2. Since you have while loop,

while($row = mysql_fetch_array($result) && $counter < 1)
{
$counter++;
First Name:<?php echo $row; ?> <br>
Last Name:<?php echo $row; ?> <br>
Address:<?php echo $row; ?> <br>
E-Mail:<?php echo $row; ?> <br>
Gender:<?php echo $row; ?> <br>
User Name:<?php echo $row; ?>
}

This part of the code will not work.
Try this.

<?php
session_start();
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);

?>
<html>
<head><title>User Account</title>
</head>
<body bgcolor="#33CCFF">
<br>Your Details Information Is Shown Below:<br><br>
<?
$counter = 0;
while($row = mysql_fetch_array($result) && $counter < 1)
{
        $counter++;
        echo "First Name: ". $row['fname'] ."<br>";
        //.. and so on...
	
}
?>
</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.