gpt4 book ai didi

php - 尝试从 SQL 查询结果中存储 session 变量

转载 作者:可可西里 更新时间:2023-11-01 07:45:21 25 4
gpt4 key购买 nike

我已经尝试了从将查询存储为变量和使用 fetch_array 的所有方法,但当我尝试打印 $_SESSION['permission'] 时,它一直只给我输出“Array”。我不知道该怎么做,请帮忙。

<?php
session_start();
mysql_connect("localhost","","");
mysql_select_db("a");
if(isset($_SESSION['loggedin']))
{
header("Location: http://www.mywebsite.com/whatever");
}
if($_POST['submit'])
{
$name = mysql_real_escape_string($_POST['username']);
$pass = mysql_real_escape_string($_POST['password']);
$mysql = mysql_query("SELECT * FROM users WHERE name = '{$name}' AND password = '{$pass}'");
$result = mysql_query("SELECT permission FROM users WHERE name = '{$name}' AND password = '{$pass}'");
$permission = mysql_fetch_array($result);
if(mysql_num_rows($mysql) < 1)
{
die("Password was incorrect!");
}
$_SESSION['loggedin'] = "YES";
$_SESSION['name'] = $name;
$_SESSION['permission'] = $permission;

header ("Location: http://www.mywebsite.com/whatever");
}
echo "<form type='login.php' method='POST'>
Username: <br>
<input type='text' name='username'><br>
Password: <br>
<input type='password' name='password'><br>
<input type='submit' name='submit' value='Login'>
</form>";
?>

最佳答案

您需要使用循环遍历结果集,

while($row = mysql_fetch_array($result)) {
//use each row value($row) as per your need
}

所以,我相信,你想要实现这样的目标,

if(isset($_POST['submit']))
{
$name = mysql_real_escape_string($_POST['username']);
$pass = mysql_real_escape_string($_POST['password']);
$result = mysql_query("SELECT permission FROM users WHERE name = '{$name}' AND password = {$pass}'");
$permission = mysql_fetch_array($result);
$mysql = mysql_query("SELECT * FROM users WHERE name = '{$name}' AND password = '{$pass}'");
if(mysql_num_rows($mysql) < 1)
{
die("Password was incorrect!");
}

$_SESSION['loggedin'] = "YES";
$_SESSION['name'] = $name;
$i = 0;
while($row = mysql_fetch_array($result)) {
$_SESSION['permission' . $i] = $row;
$i++;
}
header ("Location: ");
}

关于php - 尝试从 SQL 查询结果中存储 session 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13824980/

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