gpt4 book ai didi

php - 如何使用 PHP 从 MySQL 获取数据?我的代码出现错误

转载 作者:行者123 更新时间:2023-11-29 11:21:21 25 4
gpt4 key购买 nike

我正在尝试从 MySQL 获取一些数据并使用 PHP 回显它。以下是我使用的代码。请检查代码并告诉我其中有什么问题。

<?php

// Get a connection for the database
require_once('mysqli_connect.php');

// Create a query for the database
$query = "SELECT first_name, last_name, email, street, city, state, zip, phone, birth_date FROM testable";

// Get a response from the database by sending the connection and the query
$response = @mysqli_query($dbc, $query);

// If the query executed properly proceed
if($response){

echo '<table align="left"
cellspacing="5" cellpadding="8">
<tr><td align="left"><b>First Name</b></td>
<td align="left"><b>Last Name</b></td>
<td align="left"><b>Email</b></td>
<td align="left"><b>Street</b></td>
<td align="left"><b>City</b></td>
<td align="left"><b>State</b></td>
<td align="left"><b>Zip</b></td>
<td align="left"><b>Phone</b></td>
<td align="left"><b>Birth Day</b></td></tr>';

// mysqli_fetch_array will return a row of data from the query until no further data is available
while($row = mysqli_fetch_array($response)){

echo '<tr><td align="left">' .
$row['first_name'] . '</td><td align="left">' .
$row['last_name'] . '</td><td align="left">' .
$row['email'] . '</td><td align="left">' .
$row['street'] . '</td><td align="left">' .
$row['city'] . '</td><td align="left">' .
$row['state'] . '</td><td align="left">' .
$row['zip'] . '</td><td align="left">' .
$row['phone'] . '</td><td align="left">' .
$row['birth_date'] . '</td><td align="left">';
echo '</tr>';
}
echo '</table>';
} else {
echo "Couldn't issue database query<br />";
echo mysqli_error($dbc);
}

// Close connection to the database
mysqli_close($dbc);
?>

我得到的这段代码的输出是:

First Name Last Name Email Street City State Zip Phone Birth Day'; // mysqli_fetch_array will return a row of data from the query // until no further data is available while($row = mysqli_fetch_array($response)){ echo '' . $row['first_name'] . '' . $row['last_name'] . '' . $row['email'] . '' . $row['street'] . '' . $row['city'] . '' . $row['state'] . '' . $row['zip'] . '' . $row['phone'] . '' . $row['birth_date'] . ''; echo ''; } echo ''; } else { echo "Couldn't issue database query "; echo mysqli_error($dbc); } // Close connection to the database mysqli_close($dbc); ?>

最佳答案

我发现了我的错误。我在浏览器中输入了错误的 URL。

网址不正确:file:///C:/xampp/htdocs/php/view.php

正确网址:http://localhost/php/view.php

代码运行得非常好,现在向我显示来自 MySQL 数据库的数据。

关于php - 如何使用 PHP 从 MySQL 获取数据?我的代码出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38846709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com