PHP从SQL数据库读取数据


php read data from sql database

我想学习php,我不能从我的数据库中读取任何数据!我知道与服务器的连接是正常的,但是这条线似乎给我带来了问题。

$result = $conn->query($sql);

$sql = "SELECT firstName, middleName, lastName FROM Base";的地方我不知道是什么问题,但任何提示或答案都是感激的。

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>My first PHP page</h1>
<?php
// connect to database
$user_name = "superUser";
$password = "";
$database = "Base"$server = "127.0.0.5";
// open connection to the server
$conn = new mysqli($server, $user_name, $password);
// echo "Connection to the server is open";
// check connetion
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
else {
    print "Connection to the server is open";
}
// load the data from the table
$sql = "SELECT firstName, middleName, lastName FROM Base";
// echo $conn->server_info;
$result = $conn->query($sql);
if ($result) {
    echo "Table was found";
}
else echo "no";
/*while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br />";
}
print $result["firstName"];// ": Number of Rows!" ;*/
// if ($result->num_rows > 0) {
// output data of each row
// close server connection
$conn->close();
?>
</body>
</html>

下面是一个mysqli连接的例子:

<?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
?>

您忘记添加数据库名称。改变这一状况,

$conn = new mysqli($server, $user_name, $password);

$conn = new mysqli($server, $user_name, $password, $database);