简体   繁体   中英

Print MySQL queries results with PHP in HTML pages

I have a simple MySQL query : SELECT COUNT(column_name) FROM my_table

How to print results (in html pages) with PHP in a preexisting DB connection? Sorry for this stupid answer, I'm just learning PHP :(

$query = "SELECT COUNT(column_name) AS count FROM my_table";
$sql = mysql_query($query);
$result = mysql_fetch_assoc($sql);
echo "<p>" . $result['count'] . "</p>";

Should do the job.

Note: I used AS count in order to access it easier in the associative array.

simplistically

<?
//Connect to the database
$sql = "SELECT COUNT(column_name) as total FROM my_table";
$result = mysql_query($sql);
$row = mysql_fetch_array($sql);
echo $row['total'];
?>
$sql = mysql_query("SELECT COUNT(column_name) AS count FROM my_table");
$result = mysql_fetch_assoc($sql);
echo '<p>'. $result['count'] .'</p>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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