所以这是我的PHP代码,我不明白为什么我的查询总是失败!我从phpMyAdmin网站复制了$sql
。但它一直都是假的。
$servername = "localhost";
$dbname = "gumdb";
$conn = mysqli_connect($servername, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
} else {
echo "Connected! <br />";
}
$sql = 'SELECT * FROM `test` WHERE 1;';
$result = mysqli_query($conn, $sql);
if ($result === FALSE) {
echo "result failed!";
die(mysql_error());
}
答案 0 :(得分:1)
我认为您遇到了这个问题,因为您没有声明MySQL连接的用户名和密码,例如:
$servername = "localhost";
$user = "root"; // Your MySQL username here
$password = "root"; // Your MySQL password here
$dbname = "gumdb";
$conn = mysqli_connect($servername,$user,$password, $dbname);
答案 1 :(得分:-1)
您的查询存在问题
$sql = 'SELECT * FROM `test` WHERE 1;';
您是否注意到了2个半克隆。请删除第一个并尝试它将工作。使用如下
$servername = "localhost";
$dbname = "gumdb";
$pass = "";
//$conn = mysqli_connect(servername, "dbuser", "dbpass", "dbname");
$conn = mysqli_connect($servername, "root", "", "gumdb");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
} else {
echo "Connected! <br />";
}
$sql = 'SELECT * FROM `test` WHERE 1';
$result = mysqli_query($conn, $sql);
if ($result === FALSE) {
echo "result failed!";
die(mysql_error());
}
else {
var_dump($result);
}
我已编辑完整代码。数据库连接也存在问题。