您好,我正在尝试创建一个登录系统,但我遇到了为用户设置权限的问题,这是我用来从phpMyAdmin数据库中找出用户名和密码的MySql语句。
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
我想知道是否有人可以帮助我编辑此代码,以便我可以回显或打印account_type到页面,这样我就可以在PHP if语句中使用它来限制用户看到的内容。
我试过了
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword' and account_type ='myaccount'";
$result=mysql_query($sql);
这是完整页面代码
<?php
ob_start();
$host="127.0.0.0"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="dbname"; // Database name
$tbl_name="table"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Define $myusername and $mypassword
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
print_r($row);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_start();
$_SESSION['username'] = $myusername;
$_SESSION['password'] = $mypassword;
//session_register("myusername");
//session_register("mypassword");
//echo "correct";
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
ob_end_flush();
?>
答案 0 :(得分:1)
您缺少mysql_fetch_array()
来电。
$sql = "SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
之后你可以访问$row
数组;看其内容使用:
print_r($row);
要访问特定字段,请使用$row['fieldname']
答案 1 :(得分:0)
您需要从sql中获取该数组。
请参阅PHP manual