gpt4 book ai didi

php - 从 MySQL 数据库中检索数据

转载 作者:行者123 更新时间:2023-11-28 23:47:53 24 4
gpt4 key购买 nike

不确定我在这里做什么。

以下代码输出数据库连接语句,但不显示任何记录(“0 个结果”):

<?php 
//Server Details
$host ="localhost";
$user = "X32284679";
$password = "X32284679";

//Connection
$dbc = mysql_pconnect($host,$user,$password);

//Database Selection
$dbname="X32284679";
mysql_select_db($dbname);

if (!$dbc)
{
die("Connection Failed: " .mysqli_connect_error());
}
echo "Connected";


$sql = "select * from staff";
$result = $dbc -> query($sql);

if ($result ->num_rows >0 )
{
echo"<table><tr><th>Email</th><th>Name</th><th>Mobile</th> <th>Address</th><th>Password</th></tr>";

while($row = $result-> fetch_assoc())
{
echo "<tr><td>" .$row["staff_email"]."</td><td>".$row["staff_name"]."</td><td>".$row["staff_mobile"]."</td><td>".$row["staff_address"]."</td><td>".$row["staff_password"]."</td></tr>";
}
echo "</table>";
}
else
{
echo "0 Results";
}
$dbc->close();

?>

最佳答案

这看起来像是从教程中复制和粘贴的大量内容。

同时使用 mysql_mysqli_ 函数几乎毫无用处..

因为它看起来像您想要在 mysql_ 中使用它,所以我重新编写了您的代码以满足您的需要。

代码:

<?php 
//Server Details
$host ="localhost";
$user = "X32284679";
$password = "X32284679";

//Connection
mysql_connect($host,$user,$password) or die("An error occured while connecting...");

//Database Selection
$dbname="X32284679";
mysql_select_db($dbname);




$sql = "select * from staff";
$Query = mysql_query($sql);

if (mysql_num_rows($Query)){


$html .= "<table><tr><th>Email</th><th>Name</th><th>Mobile</th> <th>Address</th><th>Password</th></tr>";

while($row = mysql_fetch_array($Query)){

$html .= "<tr><td>" .$row["staff_email"]."</td><td>".$row["staff_name"]."</td><td>".$row["staff_mobile"]."</td><td>".$row["staff_address"]."</td><td>".$row["staff_password"]."</td></tr>";
}

$html .= "</table>";
}

else{
$html .= "0 Results";
}

echo $html;

mysql_close();

?>

关于php - 从 MySQL 数据库中检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33251282/

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