它无法正常工作。我使用的代码是
<?php
$conn=mysqli_connect('127.0.0.1','root','');
if(!$conn)
{
echo "Connection Not Established";
}
if(!mysqli_select_db($conn,'lpractice'))
{
echo "Database Not Found!";
}
$res=mysqli_query($conn,"select * from signup");
echo "<table>";
while($row= mysql_fetch_array($res))
{
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['sex'] . "</td>";
echo "</tr>"
}
echo "</table>";
?>
输出结果是
"; while($row= mysql_fetch_array($res)) { echo ""; echo "" . $row['name'] . ""; echo "" . $row['email'] . ""; echo "" . $row['age'] . ""; echo "" . $row['sex'] . ""; echo "" } echo ""; ?>
答案 0 :(得分:1)
mysqli_fetch_array
而不是mysql_fetch_array
而你在;
echo "</tr>"
答案 1 :(得分:1)
我认为根本问题与mysql无关。如果您的输出在<table>
- 标记之后开始,则看起来您的代码已经由php解释器运行而不是。例如,当您使用浏览器直接访问php文件时会发生这种情况。
使用php的常用设置是网络服务器。您可以使用多种解决方案。例如,xampp是一个易于使用的Web服务器,可用于您自己的计算机进行测试和开发。根据您的操作系统,可能有更好的解决方案(例如Linux或Mac上的预配置软件包)。
答案 2 :(得分:0)
//Try this
<?php
#create connection
$host_name = "127.0.0.1";
$username = "root";
$password = "";
$database_name = "lpractice";
$connection = mysqli_connect("$host_name","$username","$password","$database_name");
#check connection
if(mysqli_connect_error()){
echo "Connection Not Established. " .mysqli_connect_error();
}
$query = "SELECT * FROM signup";
$result = $connection->query($query);
if ($result->num_rows > 0) {
//echo table header
echo"
<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Age</th>
<th>Sex</th>
</tr>
</thead>
";
while($row = $result->fetch_assoc()) {
// output data of each row
echo"<tbody>";
echo"<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['sex'] . "</td>";
echo"</tr>";
echo"</tbody>";
echo"</table>";
}
} else {
echo "No records found.";
}
$connection->close();
?>