gpt4 book ai didi

php - 如何在读取 session 值后执行 mysql 查询

转载 作者:行者123 更新时间:2023-12-01 00:32:47 25 4
gpt4 key购买 nike

我正在使用这个脚本在 session 中读取一个值,然后我想查询数据库中的这个值:

    <?php
session_start();
$user_id = $_SESSION["user_id"] ;
echo "Your user_id is: $user_id \n" ;

$query = sprintf("SELECT * FROM `customers` WHERE id='%s' ", $user_id);
$result = mysql_query($query);

if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}

while ($row = mysql_fetch_assoc($result)) {
$email = $row['email'] . "\n";
echo "Your e-mail adress is : $email \n" ;
}
?>

当我用浏览器测试页面时,我得到了这个:您的 user_id 是:1430

无效查询:用户 'nobody'@'localhost' 的访问被拒绝(使用密码:NO)整个查询:SELECT * FROM customers WHERE id='1430'

非常感谢!

最佳答案

首先连接到您的数据库服务器并使用 mysql_connect 选择一个数据库和 mysql_select_db . mysql_query 函数将尝试使用默认参数打开连接,或者如手册所述:

If no such link is found, it will try to create one as if mysql_connect() was called with no arguments.

因此,PHP 尝试使用用户名“nobody”和无密码连接到本地主机。

mysql_connect('localhost', 'mysql_user', 'mysql_password');
mysql_select_db('my_database');

..你准备好了。

关于php - 如何在读取 session 值后执行 mysql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3742259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com