简体   繁体   English

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

[英]importing data from SQL database into html table

I am trying to pull information from my SQL database into an html table. 我试图将信息从我的SQL数据库提取到html表中。 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. 当我尝试这样做时,我得到“ 0结果”,我能够连接到我的数据库,并且SQL在MySQL Workbench中运行也很好。 It appears that $result is not greater than 0 and I am not sure why that would be the case. 看来$ result不大于0,但我不确定为什么会这样。 It was working previously when I didn't include Joins in my SQL query, however like i said it works in MySQL workbench fine. 当我在SQL查询中不包含Joins时,它就可以正常工作,但是就像我说的那样,它可以在MySQL工作台中正常工作。

<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>

The mysqli_connect() function has 4 parameters, the 4th is the name of your database mysqli_connect()函数有4个参数,第4个是数据库的名称

<?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;
}

adding php code that used for to get data from the table embed inside html file is not a good thing.so it's better if you're using separate php file to retrieve data from the table in a way that you can protect your database's table information. 添加用于从嵌入html文件中的表中获取数据的php代码不是一件好事。因此,最好使用单独的php文件以保护数据库的表信息的方式从表中检索数据。

I used a php file to retrieve data and converted that result as json data output in php file.that json output read from 'setData.js' file and that data set for the table inside the html file. 我使用了一个php文件来检索数据,并将结果转换为php文件中的json数据输出。该json输出从'setData.js'文件中读取,并且该数据集用于html文件中的表格。

i added that example into my github,so that you can refer it. 我将该示例添加到我的github中,以便您可以引用它。 here's my github repository . 这是我的github仓库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM