I'm using PHP to connect to a database, and pulling data from the database using SQL queries. I now want to be able to display the query results in HTML tables on my web page. Just wondering if someone could help me out with this?
The code I currently have is connecting to the database, and retrievning the information I want (it's not missing any info or getting me anything that I didn't ask for), but it's just displaying it in one very long paragraph with no formatting at all. I'd be grateful if someone could let me know how best to display the query results in an HTML table.
My code is below:
Code: Select all
<?php
echo extension_loaded('pgsql');
$conn_string = "host=***** port=**** dbname=***** user=***** password=*****";
$dbconn = pg_connect($conn_string);
if(!$dbconn){
die('Could not connect: ' . pg_error());
}
// generate and execute a simple query
$query = "SELECT * FROM CSGames";
$result = pg_query($dbconn, $query) or die("Error in query: $query." . pg_last_error($dbconn));
// Get the number of rows in the resultset
$rows = pg_num_rows($result);
echo "\n There are currently $rows records in the database.";
// query to retrieve titles of all games from database, with their price and description
$gameTitlesQuery = "SELECT title, price, description FROM CSGames";
$gameTitlesQueryResult = pg_query($dbconn, $gameTitlesQuery) or die("Error in query: $gameTitlesQuery." . pg_last_error($dbconn));
// now display query results in a table
while($myRow = pg_fetch_assoc($gameTitlesQueryResult)){
"INSERT INTO resultsTable (column1, column2, column3)
VALUES(title, price, description)";
printf("<tr><td>%s</td><td>%s</td<td>%s</td></tr>", $myRow['title'], htmlspecialchars ($myRow[price]), htmlspecialchars($myRow['description']));
}
// close database connection
// pg_close($dbconn);
?>