gpt4 book ai didi

php - 将 $_SESSION[] 数据制作成 php 变量,但它仍然无法在 mysql 查询中工作

转载 作者:行者123 更新时间:2023-11-29 01:07:22 26 4
gpt4 key购买 nike

我在检查 mysql 查询的 $_SESSION 变量时遇到了问题。我想做的是获取登录用户的详细信息,但它似乎无法正常工作。

我有 $user = mysql_real_escape_string($_SESSION['username']); 将代码放入常规变量中,然后我查询数据库是:$sql = "SELECT * FROM admin WHERE username='$user' LIMIT 1";

为了计算用户是否存在,我使用代码:$userCount = mysql_num_rows($sql);//统计输出量

这似乎行不通。我不断收到此错误:“警告:mysql_num_rows() 期望参数 1 为资源,第 18 行/home/alexartl/public_html/CRM/headercode.php 中给出的字符串”

顺便说一句,当我测试这个时,用户帐户确实存在并且已登录下面是完整的代码

  // If the session vars aren't set, try to set them with a cookie
if (!isset($_SESSION['user_id'])) {
if (isset($_COOKIE['user_id']) && isset($_COOKIE['username'])) {
$_SESSION['user_id'] = $_COOKIE['user_id'];
$_SESSION['username'] = $_COOKIE['username'];
}
}
?>
<?php
//if the username is set
if (isset($_SESSION['username'])) {
//making the username into a php variable
$user = mysql_real_escape_string($_SESSION['username']);
//the query to grab the users name
$sql = "SELECT * FROM admin WHERE username='$user' LIMIT 1";
$userCount = mysql_num_rows($sql); // count the output amount
if ($userCount == 1) {
while($row = mysql_fetch_array($sql)){
//just the array that grabs all the users info
$username = $row["username"];
$password = $row["password"];
$first_name = $row["first_name"];
$last_name = $row["last_name"];
$gender = $row["gender"];
$birthdate = $row["birthdate"];
$email_address = $row["email_address"];
$city = $row["city"];
$state = $row["state"];
$retrieval = $row["retrieval"];
$isAdmin = $row["isAdmin"];
$join_date = $row["join_date"];


//if the user has "isAdmin" as "Yes", then this link to a "manage Users" page will appear
if($isAdmin == "Yes"){
$ifAdmin = '<li><a href="manageUsers.php">Manage Users</a></li>';
}
}
}
}
?>

最佳答案

我不会进入“不要使用 mysql_* 命令”,但不要 :P

你错过了:

 $result = mysql_query($sql);  //Actually execute the query

然后用作

$userCount = mysql_num_rows($result); // count the output amount

另外,您似乎也没有连接到或使用您希望查询的数据库。

$link = mysql_connect('localhost', 'user', 'pass') or die('Could not connect to mysql server.' );
mysql_select_db('databaseName');

关于php - 将 $_SESSION[] 数据制作成 php 变量,但它仍然无法在 mysql 查询中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15827807/

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