简体   繁体   中英

Error in retriving data from database

I'm trying to retrive my first name and last name for viewprofile.php but i'm getting resource ID#5 . I am CREATING session in Login page after successful authentication. And I am trying to use it here. I'm trying to use a session which has been created to fetch the data from the database.

<html>
<h1> My Profile</h1>
<?php
session_start();
require "config.php";
$con = mysql_connect("localhost", $db_user, $db_pass);

if(!$con)
    {
die('cound not connect: '. mysql_error());
    }

mysql_select_db($db_name, $con);

# include 'new.php'; 
echo $_SESSION['username'];
#$usname = $_SESSION['username'];
#echo 'local var: ',  $usname;
$sql1 = "SELECT * FROM register WHERE uname='".$_SESSION['username']."'" ;

#echo $sql1 ;
$result= mysql_query($sql1)or die(mysql_error());
#echo "res: " . $result;

while($row = mysql_fetch_array($result))
{
echo $row['fname']." ".$row['lname'];
echo"</br>";
}

mysql_close($con); 

?>

</html>

I'm getting resource ID#5. Searched numerous places no luck. Kindly help

Unless I'm mistaken mysql_fetch_array will give you values that can be expressed as $row[0], $row[1] etc whereas mysql_fetch_assoc is what you're trying to use to get $row['fname'] etc

Change your code to

while($row = mysql_fetch_assoc($result))

In either case a print_r($row) within your while loop will you how it's made up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM