Php+mysqli implements the method of printing a table information in a database into a table _php tips

Source: Internet
Author: User
Tags php programming

The example in this article describes how the PHP+MYSQLI implementation prints a table information in a database (including the header) to a table. Share to everyone for your reference. Specifically as follows:

This piece of code will look at it. Need to learn basic knowledge. The code is as follows:

Copy Code code as follows:
<?php
$mysqli = new Mysqli ("localhost", "root", "123456", "Liuyan");
if (! $mysqli) {
Die ($mysqli->error);
}
function showtable ($mysqli, $table _name) {
$sql = "SELECT * from $table _name";
$res = $mysqli->query ($sql);
Gets the total number of rows and columns returned
echo "Altogether:". $res->num_rows. " All: ". $res->field_count." Column. ";
Get header---Remove from $res
echo "<table border=1><tr>";
while ($field = $res->fetch_field ()) {
echo "<th>{$field->name}</th>";
}
echo "</tr>";
Looping out data
while ($row = $res->fetch_row ()) {
echo "<tr>";
foreach ($row as $value) {
echo "<td> $value </td>";
}
echo "</tr>";
}
echo "</table>";
$res->free ();
}
Showtable ($mysqli, "News");
?>

I hope this article will help you with your PHP programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.