从数据库检索信息(1 个字段/对象;无数组)

发布于 2024-12-08 10:21:00 字数 971 浏览 3 评论 0原文

我正在为我的网站做一种登录页面,我现在正在测试它,这个代码是在登录之后。问题是,我想从数据库中检索一些信息,但代码不起作用(它不回显任何内容)。我检查了 MySQL 查询正常并且有效,但 PHP 中没有结果。

请我很高兴获得任何帮助和答案,

//---MySQL connection---//
$server = "localhost";
$user = "root";
$pass = "password";
$db = "users";
$table = "users";
mysql_connect($server,$user,$pass) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
//----------------------//

//---MySQL query, data selection---//
$sesuser = $_SESSION['username'];
$sql = "SELECT data FROM $table WHERE username='$sesuser'";
$predata = mysql_query($sql);
$data = mysql_fetch_field($predata);
//---------------------------------//

//---Check if session is registered---//
session_start();
if(session_is_registered("username")){
    echo "\n"."Hello ".$_SESSION["username"]."<br />";
    echo $data; //!!this line doesn't work
}
else{
    echo "<script>window.location=/login/</script>";
}
//------------------------------------//
?>

I'm doing a sort of login page for my website which I'm just testing right now, and this code is after the login. The thing is, I want to retrieve some information from my database, but the code doesn't work (it doesn't echo anything). I checked that the MySQL query is fine and it works, but there is no result in the PHP.

Please I would be happy for any help and answers,

//---MySQL connection---//
$server = "localhost";
$user = "root";
$pass = "password";
$db = "users";
$table = "users";
mysql_connect($server,$user,$pass) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
//----------------------//

//---MySQL query, data selection---//
$sesuser = $_SESSION['username'];
$sql = "SELECT data FROM $table WHERE username='$sesuser'";
$predata = mysql_query($sql);
$data = mysql_fetch_field($predata);
//---------------------------------//

//---Check if session is registered---//
session_start();
if(session_is_registered("username")){
    echo "\n"."Hello ".$_SESSION["username"]."<br />";
    echo $data; //!!this line doesn't work
}
else{
    echo "<script>window.location=/login/</script>";
}
//------------------------------------//
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤千羽 2024-12-15 10:21:00

session_start() 放在顶部或在使用 $_SESSION 变量之前

还有一件事: 函数 session_is_registered 自 PHP 5.3.0 起已弃用。强烈建议不要依赖此功能。

更好的方法

session_start();
//---MySQL query, data selection---//
$sesuser = mysql_real_escape_string($_SESSION['username']);
$sql = "SELECT data FROM $table WHERE username='$sesuser'";
$predata = mysql_query($sql);
$data = mysql_fetch_field($predata);
//---------------------------------//

//---Check if session is registered---//

if(isset($_SESSION['username'])){
    echo "\n"."Hello ".htmlentities($_SESSION["username"])."<br />";
    echo $data; 
}
else{
    header("Location :"login.php");
    exit();
}

put session_start() at the top or just before you use $_SESSION variable

one more thing : The function session_is_registered has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged.

better way

session_start();
//---MySQL query, data selection---//
$sesuser = mysql_real_escape_string($_SESSION['username']);
$sql = "SELECT data FROM $table WHERE username='$sesuser'";
$predata = mysql_query($sql);
$data = mysql_fetch_field($predata);
//---------------------------------//

//---Check if session is registered---//

if(isset($_SESSION['username'])){
    echo "\n"."Hello ".htmlentities($_SESSION["username"])."<br />";
    echo $data; 
}
else{
    header("Location :"login.php");
    exit();
}
我只土不豪 2024-12-15 10:21:00

var_dump($data); - 说什么?

是的,但是 session_start 在文件开头;

并尝试(通过php):

$i = 0;
while ($i < mysql_num_fields($result)) {
    echo "Information for column $i:<br />\n";
    $meta = mysql_fetch_field($result, $i);
    if (!$meta) {
        echo "No information available<br />\n";
    }
    echo "<pre>
blob:         $meta->blob
max_length:   $meta->max_length
multiple_key: $meta->multiple_key
name:         $meta->name
not_null:     $meta->not_null
numeric:      $meta->numeric
primary_key:  $meta->primary_key
table:        $meta->table
type:         $meta->type
unique_key:   $meta->unique_key
unsigned:     $meta->unsigned
zerofill:     $meta->zerofill
</pre>";
    $i++;
}

如果你改变< code>mysql_fetch_field 到 mysql_fetch_row 您将能够通过以下方式访问数据:$data[0];

var_dump($data); - What is says?

And YES, but session_start at begining of file;

And try(via php):

$i = 0;
while ($i < mysql_num_fields($result)) {
    echo "Information for column $i:<br />\n";
    $meta = mysql_fetch_field($result, $i);
    if (!$meta) {
        echo "No information available<br />\n";
    }
    echo "<pre>
blob:         $meta->blob
max_length:   $meta->max_length
multiple_key: $meta->multiple_key
name:         $meta->name
not_null:     $meta->not_null
numeric:      $meta->numeric
primary_key:  $meta->primary_key
table:        $meta->table
type:         $meta->type
unique_key:   $meta->unique_key
unsigned:     $meta->unsigned
zerofill:     $meta->zerofill
</pre>";
    $i++;
}

And if you change mysql_fetch_field to mysql_fetch_row you would be able to reach your data over: $data[0];

你没皮卡萌 2024-12-15 10:21:00

基本上在 $data 中有一个返回的对象,你可以像这样回显它
$数据->名称

Basically there is an object returned in $data and you can echo it like
$data->name

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文