湖濱散記部落格的樹心幽徑[login][主頁]
561:20200108設計PHP函數dumpTable,用來列出selldb資料庫的custs資料表的全部資料錄

(1) code : c2.php

<body bgcolor=lightblue>
<?php
function executeSQL($con,$sql){
    if( $query= mysqli_query($con, $sql) ){ echo " [$sql] OK!<br> "; }
    else { echo " [$sql] Failure!<br> "; }
    return $query;
}

function dumpTable($table,$con,$bgcolor){
    $sql= "select * from $table";
    $query= executeSQL($con,$sql);
    echo "<table bgcolor=$bgcolor border=3>";
    echo "<tr>";
    $fc= mysqli_num_fields($query) ;
    while ($field = mysqli_fetch_field($query)){
        printf("<td>%s", $field->name);
    }
    $rowc=0;
    while ($row = mysqli_fetch_array($query)){
        echo "<tr>";
        for ($i=0;$i<$fc;$i++){
            echo  "<td>" . $row[$i];
        }
        $rowc++;
    }
    echo "</table>";
    echo "total $rowc records<br><br>";
}

$con = @mysqli_connect('localhost', 'root', 'w?????', 'selldb');
if (!$con) { echo "Error: " . mysqli_connect_error(); exit(); }
$sql = 'set names utf8;';
 
$query= executeSQL($con,$sql);
dumpTable("custs",$con,"red");
dumpTable("custs",$con,"green");
dumpTable("custs",$con,"blue");
mysqli_close ($con);

?>
</body>

(2)執行結果

[set names utf8;] OK!
[select * from custs] OK!

id name amount tel
10001 小草 1000 08-233375
10002 天牛 3000 082-20488
10003 小吉 5000 24680
10003 小吉 5000 24680
10003 小吉 5000 24680
10003 小吉 5000 24680
10003 小吉 5000 24680
10003 小吉 5000 24680
10003 小吉 5000 24680
10003 小吉 5000 24680
10003 小吉 5000 24680
total 11 records

[select * from custs] OK!
id name amount tel
10001 小草 1000 08-233375
10002 天牛 3000 082-20488
10003 小吉 5000 24680
10003 小吉 5000 24680
10003 小吉 5000 24680
10003 小吉 5000 24680
10003 小吉 5000 24680
10003 小吉 5000 24680
10003 小吉 5000 24680
10003 小吉 5000 24680
10003 小吉 5000 24680
total 11 records

[select * from custs] OK!
id name amount tel
10001 小草 1000 08-233375
10002 天牛 3000 082-20488
10003 小吉 5000 24680
10003 小吉 5000 24680
10003 小吉 5000 24680
10003 小吉 5000 24680
10003 小吉 5000 24680
10003 小吉 5000 24680
10003 小吉 5000 24680
10003 小吉 5000 24680
10003 小吉 5000 24680
total 11 records

select id,article_id,topic,text from lt_articles_text where article_id =561; ok. update lt_articles set num_reads=num_reads +1 where id=561; ok.