我正在运行LAMP服务器并且我试图在网页上显示我的数据库表
这是我的php脚本
$servername = "127.0.0.1";
$username = "root";
$password = "none";
$dbname = "chegada";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else{echo "OK!";}
$sql = "SHOW TABLES FROM $dbname";
$result = mysql_query($sql);
if (!$result) {
echo "DB Error, could not list tables\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
网页显示了这个
警告:mysqli_connect():( HY000 / 2002):拒绝连接 第16行/var/www/html/index.php
注意:尝试获取非对象的属性 第17行/var/www/html/index.php
OK!
致命错误:未捕获错误:调用未定义函数mysql_query() 在/var/www/html/index.php:22堆栈跟踪:#0 {main}被投入 第22行/var/www/html/index.php
有人可以帮我解决这个问题吗?
答案 0 :(得分:0)
试试这个
$servername = "127.0.0.1";
$username = "root";
$password = ""; // maybe this should be an empty string if you dont have password setup
$dbname = "chegada";
$conn = mysqli_connect($servername, $username, $password, $dbname); // this should be mysqli_query
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else{echo "OK!";}
$sql = "SHOW TABLES FROM $dbname";
$result = mysqli_query($conn,$sql);
if (!$result) {
echo "DB Error, could not list tables\n";
echo 'MySQL Error: ' . mysqli_error();
exit;
} else {
$res = mysqli_fetch_all($result);
var_dump($res);
}
答案 1 :(得分:0)
使用mysqli_query
$result = mysqli_query($conn,$sql);
答案 2 :(得分:0)
我怀疑你有一个默认的本地mysql,在这种情况下,密码“none”是一个空字符串。字面上没有密码,不是“无”字样。你需要:
$servername = "127.0.0.1";
$username = "root";
$password = "";
$dbname = "chegada";
你的第二个错误是由你在mysqli_中开始但后来恢复到mysql_的一半造成的。