从数据库中检索数据时php中的问题

时间:2011-09-16 04:42:56

标签: php mysql database

我有一个关于从数据库中检索数据的问题。我在userrecord中创建了一个数据库,其表名是tbl_user。我试图在他们登录系统时显示用户信息。即查看记录的用户配置文件代码如下:

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

代码echo $ row ['']不显示数据库中的记录。

3 个答案:

答案 0 :(得分:1)

  

,其表名为 tbl_user

你的SQL是table_user

无论如何,我没有看到其他人误以为你的代码,就是这样。如果是拼写错误,请尝试调试连接,并向我们提供错误消息...

答案 1 :(得分:1)

尝试:

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

答案 2 :(得分:0)

不需要这个@符号:

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

校正:

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