我正在尝试使用php中的代码从mysql数据库中选择数据,但总是有错误。
<?php
$dbhost = "localhost";
$dbuser = "";
$dbpass = "";
$db = "test";
$connect = mysql_connect($dbhost, $dbuser, $dbpass, $db)
or die ("connexion impossible");
mysql_select_db($db) or die ("selection de la base échoué");
$username = $_POST['username'];
$password = $_POST['password'];
query = mysql_query("SELECT * FROM table2 WHERE username= '$username' AND password='.$password'");
$num = mysql_num_rows($query);
if($num == 1) {
while($list = mysql_fetch_assoc($query)){
$output = $list;
echo json_encode($output);
}
mysql_close();
}
?>
错误:
Notice: Undefined variable: username in C:\wamp\www\projet\connect.php on line 11
Notice: Undefined variable: password in C:\wamp\www\projet\connect.php on line 11
答案 0 :(得分:0)
替换
行query = mysql_query("SELECT * FROM table2 WHERE username= '$username' AND password='.$password'");
带
$query = mysql_query("SELECT * FROM table2 WHERE username= '".$username."' AND password='".$password."'");
你错过了$ before变量查询,并且遇到了一些字符串连接问题
请使用PDO而不是弃用的mysql _ *
答案 1 :(得分:-1)
在mysql查询中$password
之前有一个额外的点,而query
应该是$query
,因为$query
是可变的。
query = mysql_query("SELECT * FROM table2 WHERE username= '$username' AND password='.$password'");
应该是
$query = mysql_query("SELECT * FROM table2 WHERE username= '$username' AND password='$password'");