dongsun5330 2016-11-15 23:52
浏览 262
已采纳

将数据从SQL数据库导入到html表中

I am trying to pull information from my SQL database into an html table. When I attempt this I get "0 results" however, I am able to connect to my DB, and also the SQL runs in MySQL Workbench completely fine. It appears that $result is not greater than 0 and I am not sure why that would be the case. It was working previously when I didn't include Joins in my SQL query, however like i said it works in MySQL workbench fine.

<html>
<head><title>Employee</title>
</head>
<pre>
<body>
  <center><strong><a href="manager.html">Main Page</a></strong></center>
  <?php
$servername = "localhost";
$username = "root";
$password = "root";
$conn = new mysqli($servername,$username,$password);
if($conn->connect_error){
  die("connection failed: " . $conn->connect_error);
}
$sql = "SELECT first_name, last_name, email, address.address,
address.district, address.postal_code, address.phone, country.country
FROM staff
 JOIN address ON staff.address_id = address.address_id
 JOIN city ON address.city_id = city.city_id
 JOIN country ON city.country_id = country.country_id";
$result = $conn->query($sql);

if ($result->num_rows > 0) {

    echo"<table>";
    echo("<table border = \"1\">");
    print("<tr>");
    print("<th>First Name</th>");
    print("<th>Last Name</th>");
    print("<th>Email</th>");
    print("<th>Address</th>");
    print("<th>District</th>");
    print("<th>Postal Code</th>");
    print("<th>Phone</th>");
    print("<th>Country</th>");
    while($row = $result->fetch_assoc()) {
        echo "<tr><td>" . $row["staff.last_name"]. "</td><td>" . $row["staff.first_name"].
        "</td><td>" . $row["staff.email"]. "</td><td>" . $row["address.address"] . "</td><td>" .
        $row["address.district"] . "</td><td>" . $row["address.postal_code"] . "</td><td>" .
        $row["address.phone"] . "</td><td>" . $row["country.country"] . "</td></tr>";
    }
} else {
    echo "0 results";
}
echo"</table>";
$conn->close();

?>

</body>
</pre>
</html>
  • 写回答

2条回答 默认 最新

  • dragonmeng2002 2016-11-16 00:06
    关注

    The mysqli_connect() function has 4 parameters, the 4th is the name of your database

    <?php
    $servername = "localhost";
    $username = "root";
    $password = "root";
    $dbname = 'Something';
    
    $conn = new mysqli($servername,$username,$password, $dbname);
    

    If in doubt, See the manual

    You should also get into the habit of testing if a database access has worked or not before continuing, the error message is normally very good at helping you debug your code

    $result = $conn->query($sql);
    if (!$result) {
        echo $conn->error;
        exit;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制