开发者

Outputting a list of MySQL table values in PHP/HTML

开发者 https://www.devze.com 2023-02-03 16:41 出处:网络
I have a MySQL list with a few categ开发者_运维百科ories and a lot of rows of data. I want to simply output that in PHP/HTML. How would I do that?<?php

I have a MySQL list with a few categ开发者_运维百科ories and a lot of rows of data. I want to simply output that in PHP/HTML. How would I do that?


<?php 
 $query = "SELECT * FROM TABLE";

 $res = mysql_query($query,$connection);

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

 print_r($row);

}
?>


To expand on what was already said: http://www.anyexample.com/programming/php/php_mysql_example__display_table_as_html.xml

This will produce a nice html table out of a query.


Please note that the mysql_* functions, as offered by some answers to this question, have been deprecated for quite some time. It is recommended to use either the mysqli_* functions (MySql Improved, which uses a newer underlying library for accessing mysql), or the PDO (PHP Data Objects, an object-oriented interface for connecting to various databases).

For example:

// Create a new PDO connection to localhost.
$dbh  = new PDO("mysql:localhost;dbname=testdb",  $user, $password);
// Create a PDO Statement object based on a query.
// PDO::FETCH_ASSOC tells PDO to output the data as an associative array.
$stmt = $dbh->query("SELECT * FROM Table", PDO::FETCH_ASSOC);
// Iterate over the statement, using a simple foreach
foreach($stmt as $row) {
    echo $row['column1'].' and '.$row['column2'];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消