我已经尝试了将查询存储为变量并使用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>";
?>
答案 0 :(得分:0)
您需要使用循环遍历结果集
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: ");
}
答案 1 :(得分:0)
如果$row
是查询的结果,则需要指定$_SESSION['permission'] = $row[column_name];
答案 2 :(得分:0)
刚编辑了代码的POST部分。请记住,您必须获取结果并使用关联数组来获取特定列的值:
if($_POST['submit'])
{
$name = mysql_real_escape_string($_POST['username']);
$pass = mysql_real_escape_string($_POST['password']);
$result = mysql_query("SELECT * FROM users WHERE name = '{$name}' AND password = {$pass}'");
if(!$row = mysql_fetch_array($result))
{
die("Password was incorrect!");
}
$_SESSION['loggedin'] = "YES";
$_SESSION['name'] = $name;
$_SESSION['permission'] = $row['permission'];
header ("Location: ");
}
无需再运行两次查询。
答案 3 :(得分:0)
您正在将行数组分配给$_SESSION['permission']
变量,因此当您打印它时会得到数组。
尝试在该阵列上使用print_r,您将看到其中的内容。示例:print_r($_SESSION['permission']);
Print无法打印数组。使用print_r
或var_dump
了解当您再次发现这种情况时会发生什么。