我可以连接到数据库,但是它说我的查询出了点问题,但是我确定没有。这是因为在我的phpmyadmin控制台中进行了estest处理,并且它给了我所需的结果。我不确定我是否以正确的方式制作桌子已经一段时间了。
尝试了几种conn方法并使用diff查询
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Display customer Data</title>
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
// Check connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = " SELECT *FROM customer ";
if ($conn->query($sql) === TRUE) {
echo "Your customers are being displayed";
echo"<table border = '1' ";
echo "<tr><tr>Customer ID</tr><td>Customer First Name</td><td>Customer Last Name</td><td></td><td>Street Address</td><td>State</td><td>City</td><td>ZIP</td><td>Company Name</td><tr>phone</tr><tr>Phone EXT</tr></tr>";
while($row = mysqli_fetch_assoc($conn)){
echo "<tr><td>{$row[`customerID`]}</td><td>{$row[`firstName`]}</td><td>{$row[`lastName`]}</td><td>{$row[`streetAddress`]}</td><td>{$row[`State`]}</td><td>{$row[`City`]}</td><td>{$row[`Zip`]}</td><td>{$row[`companyName`]}</td><td>{$row[`phone`]}</td><td>{$row[`phoneExt`]}</td></tr>";
}
echo"</table>";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
</body>
</html>`
也许我的mysqli提取有问题吗?
答案 0 :(得分:1)
在$ conn-> query($ sql)中显示面向对象的样式,而mysqli_fetch_assoc($ conn)显示过程样式。
If you want to go with object oriented then :
mysqli();
$result = $conn->query($sql);
While($row = $result->fetch_assoc()){//code}
If you want to go with procedural style:
mysqli_connect();
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result)){//code}