0

我正在尝试使用 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
4

2 回答 2

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."'");

您错过了变量查询之前的 $,并且存在一些字符串连接问题

请使用PDO而不是弃用的 mysql_*

于 2012-06-14T08:50:28.777 回答
-1

$password在 mysql 查询之前有额外的点,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'");
于 2012-06-14T08:55:15.170 回答