Home > Backend Development > PHP Tutorial > PHP connects to the database and reads the data and prints it

PHP connects to the database and reads the data and prints it

WBOY
Release: 2016-07-29 09:05:51
Original
2094 people have browsed it

<code><span><span><?php</span><span>$link</span> = mysqli_connect(<span>"localhost"</span>,<span>"root"</span>,<span>"root"</span>) <span>or</span><span>die</span>(<span>"Connect error"</span>);
<span>if</span>(<span>$link</span>){
    <span>echo</span><span>"Connect success<br>"</span>;
    mysqli_select_db(<span>$link</span>, <span>"world"</span>);
    <span>$sql</span> = <span>"select * from city"</span>;
    <span>$result</span> = mysqli_query(<span>$link</span>, <span>$sql</span>);
    <span>$rows</span> = mysqli_num_rows(<span>$result</span>);
    <span>$cols</span> = mysqli_num_fields(<span>$result</span>);
    <span>echo</span><span>"rows are "</span>.<span>$rows</span>.<span>", cols are "</span>.<span>$cols</span>.<span>"<br>"</span>;
    <span>echo</span><span>"<table border='1' align='center' width='80%'>"</span>;
    <span>echo</span><span>"<caption>City info</caption>"</span>;
    <span>echo</span><span>"<th>ID</th><th>Name</th><th>CountryCode</th><th>District</th><th>Population</th>"</span>;
    <span>while</span> (<span>$row</span>=mysqli_fetch_row(<span>$result</span>)){
        <span>echo</span><span>"<tr align='center'>"</span>;
        <span>foreach</span> (<span>$row</span><span>as</span><span>$data</span>){
            <span>echo</span><span>"<td>"</span>.<span>$data</span>.<span>"</td>"</span>;
        }
        <span>echo</span><span>"</tr>"</span>;
    }
    <span>echo</span><span>"</table>"</span>;
}<span>else</span> {
    <span>echo</span><span>"Connect error"</span>;
}
<span>?></span></span></code>
Copy after login
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces PHP to connect to the database and read data and print, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

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