AKor
AKor

Reputation: 8882

Outputting a list of MySQL table values in PHP/HTML

I have a MySQL list with a few categories and a lot of rows of data. I want to simply output that in PHP/HTML. How would I do that?

Upvotes: 0

Views: 1685

Answers (3)

Michael
Michael

Reputation: 1866

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

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

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

 print_r($row);

}
?>

Upvotes: 1

kander
kander

Reputation: 4286

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'];
}

Upvotes: 0

Yacine Filali
Yacine Filali

Reputation: 1762

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.

Upvotes: 0

Related Questions