我的PHP如下。这总共是两个文件
对于“ dbh.php”:
<?php
$server = "localhost";
$username = "root";
$password = "";
$name = "login_system";
$con = mysqli_connect("localhost","username","","login_system");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
对于“ index3.php”:
<?php
include'dbh.php';
?>
<!DOCTYPE html>
<html>
<head>
<title> It is index 3</title>
</head>
<body>
<?php
global $con;
$sql = "SELECT * FROM users;";
$result = mysqli_query($con,$sql );
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0){
while($row = mysqli_fetch_assoc(result)){
echo $row['useruid'] . "<br>";
}
}
?>
</body>
</html>
执行此操作时出现错误。
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\phplesson\index3.php on line 13
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\phplesson\index3.php on line 14
这是否意味着我遇到了连接问题?还是其他问题?
另外,我该如何解决?
感谢帮助!
答案 0 :(得分:0)
在下面替换“ dbh.php”的代码:
<?php
$server = "localhost";
$username = "root";
$password = "";
$db_name = "testc";
$con = mysqli_connect($server,$username,$password,$db_name);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
在下面替换“ index3.php”的代码:
<?php
include'db.php';
?>
<!DOCTYPE html>
<html>
<head>
<title> It is index 3</title>
</head>
<body>
<?php
$sql = "SELECT * FROM users;";
$result = mysqli_query($con,$sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0){
while($row = mysqli_fetch_assoc($result)){
echo $row['useruid'] . "<br>";
}
}
?>
</body>
</html>