Home > Backend Development > PHP Problem > Implementation method of php mysql query display

Implementation method of php mysql query display

藏色散人
Release: 2023-03-04 07:04:02
Original
3479 people have browsed it

php Mysql query display implementation method: first define a "ShowTable" method; then connect to the database through "mysql_connect"; then use "mysql_query" and other functions to query the database; finally, display and output the query results in a table .

Implementation method of php mysql query display

Recommended: "PHP Video Tutorial"

PHP queries the MySql database and outputs the results in a table Example

When using PHP to develop a website, MySql database query results are output in tables, which may be difficult for students who are new to PHP program development.

This article uses an example to demonstrate the function of PHP querying the MySql database and outputting the results in a table. I hope it will be helpful to everyone's PHP programming.

<strong><span style="font-size:18px;">
<?php
//PHP查询MySql数据库,将结果用表格输出
    function ShowTable($table_name){
        $conn=mysql_connect("localhost","root","toor");
        if(!$conn){
            echo "连接失败";
        }
        mysql_select_db("test",$conn);
        mysql_query("set names utf8");
        $sql="select * from $table_name";
        $res=mysql_query($sql,$conn);
        $rows=mysql_affected_rows($conn);//获取行数
        $colums=mysql_num_fields($res);//获取列数
        echo "test数据库的"."$table_name"."表的所有用户数据如下:<br/>";
        echo "共计".$rows."行 ".$colums."列<br/>";
        
        echo "<table><tr>";
        for($i=0; $i < $colums; $i++){
            $field_name=mysql_field_name($res,$i);
            echo "<th>$field_name</th>";
        }
        echo "</tr>";
        while($row=mysql_fetch_row($res)){
            echo "<tr>";
            for($i=0; $i<$colums; $i++){
                echo "<td>$row[$i]</td>";
            }
            echo "</tr>";
        }
        echo "</table>";
    }
    ShowTable("test1");
?></span></strong>
Copy after login

The above is the detailed content of Implementation method of php mysql query display. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template