Rumah > pembangunan bahagian belakang > tutorial php > PHP连接数据库并读取数据打印

PHP连接数据库并读取数据打印

WBOY
Lepaskan: 2016-07-29 09:05:51
asal
2094 orang telah melayarinya

<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>echo</span><span>"<caption>City info</caption>"</span>;
    <span>echo</span><span>"</span><th>ID</th>
<th>Name</th>
<th>CountryCode</th>
<th>District</th>
<th>Population</th>";
    <span>while</span> (<span>$row</span>=mysqli_fetch_row(<span>$result</span>)){
        <span>echo</span><span>"<tr align="center">";
        <span>foreach</span> (<span>$row</span><span>as</span><span>$data</span>){
            <span>echo</span><span>"</span><td>".<span>$data</span>.<span>"</span>
</td>";
        }
        <span>echo</span><span>"</span>
</tr>"</span>;
    }
    <span>echo</span><span>"</span>
</table>"</span>;
}<span>else</span> {
    <span>echo</span><span>"Connect error"</span>;
}
<span>?></span></span></span></code>
Salin selepas log masuk
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了PHP连接数据库并读取数据打印,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan