【问题标题】:How to show table from MySQL database using php and html如何使用 php 和 html 从 MySQL 数据库中显示表
【发布时间】:2016-07-21 08:29:25
【问题描述】:

我正在尝试将我的 html 页面与 MySQL 数据库连接,以将来自一个特定表的数据显示到我的页面,但我总是得到一个错误,实际上它只是成为 SQL 代码的一部分。我对PHP编程真的很陌生,所以请有人帮助我,我做错了什么? 这是我的代码:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>AJDE</title>
</head>
<?php
$servername = "localhost";
$username = "******";
$password = "*****";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$query = "select * from PERCENTILE";
$result = mysqli_query($conn,$query);
if(!$result) {

    die ("Umro!");

    }
/* close connection */
$mysqli->close();
?>
<body>
</body>
</html>

谢谢!

【问题讨论】:

  • 忘记选择数据库名称!!

标签: php mysql database


【解决方案1】:

您想将表格显示为 HTML 表格还是仅显示一个数组?

以下是我将表格显示为 HTML 表格的操作:

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '*****';
$dbname = 'dbname';
$selectedTable = 'whateverTableYouWant';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);
if (!$conn){
    die('cannot connect to mysql');
}

$query = "SELECT * FROM $selectedTable";
if ($result = mysqli_query($conn , $query)) {

    echo("<div class = 'data_wrapper'>");

    // Display Header of the table

    $fieldcount=mysqli_num_fields($result); //value = number of columns

    $row = mysqli_fetch_assoc($result);     //Fetch a result row as an associative array:

        //array to string conversion

        echo("<table id='example' class='table table-striped table-bordered' cellspacing='0' width='100%'>");

        echo("<thead> <tr>");

        foreach($row as $item){

            echo "<th>" .$item. "</th>";
        }

        echo("</tr> </thead>");


        //Footer
        echo("<tfoot> <tr>");

        foreach($row as $item){

            echo "<th>" .$item. "</th>";
        }

        echo("</tr> </tfoot>");



        //Display Data within the table
        echo("<tbody>");
        while ($row = mysqli_fetch_assoc($result)){
                echo "<tr>";
                foreach ($row as $item){
                    echo "<td contenteditable = 'true'>" . $item . "</td>"; //Change contenteditable later
                    //Editable data should be constricted, int = numbers only, string = words, date = date
            }
            echo "</tr>";
        }
        echo("<tbody>");
        echo "</table>";
        echo("</div>");

    }

以下只是显示为数组:

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '*****';
$dbname = 'dbname';
$selectedTable = 'whateverTableYouWant';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);

if (!$conn){
    die('cannot connect to mysql');
}
 $query = "SELECT * FROM $selectedTable";

 if ($result = mysqli_query($conn , $query)) {
    while ($row = mysqli_fetch_array($result)){
       print_r($row);
    }
 }

【讨论】:

  • 非常感谢!你的回答对我帮助很大。
  • 很高兴我能帮上忙!祝你的项目好运! :D
【解决方案2】:

请按如下所述更改连接字符串。

<?php
$con = mysqli_connect("localhost","username","password","dbname");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
?>

如果您有任何疑问,请告诉我。

【讨论】:

    【解决方案3】:

    我认为您需要选择一个用于运行查询的数据库:

    试试这个代码:

    <?php
    $username = "your_name";
    $password = "your_password";
    $hostname = "localhost"; 
    
    //connection to the database
    $dbhandle = mysql_connect($hostname, $username, $password) 
    or die("Unable to connect to MySQL");
    echo "Connected to MySQL<br>";
    
    //select a database to work with (so replace the database name examples)
    $selected = mysql_select_db("examples",$dbhandle) 
    or die("Could not select examples");
    
    //execute the SQL query and return records
    $result = mysql_query("SELECT * FROM PERCENTILE");
    
    //fetch the data from the database and display results
    //replace id,name,year with the columns you have on your table PERCENTILE and you want to show
    while ($row = mysql_fetch_array($result)) {
    echo "ID:".$row{'id'}." Name:".$row{'name'}."Year: ". $row{'year'}."<br>";
    }
    //close the connection
    mysql_close($dbhandle);
    ?>
    

    希望对你有帮助, 文斯。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-24
      • 1970-01-01
      • 1970-01-01
      • 2022-12-04
      • 2011-07-08
      • 2013-02-15
      • 1970-01-01
      相关资源
      最近更新 更多