การทำ auto refresh และการเชื่อมต่อฐานข้อมูล
ในตัวอย่างนี้แสดง how to ดังต่อไปนี้
- การทำ Auto refresh ของหน้าเว็บ
- การเชื่อมต่อฐานข้อมูล
- การใช้คำสั่ง SQL
- การอ่านข้อมูลในฐานข้อมูล
- การแปลงข้อมูลตัวอักษรเป็นตัวเลข
- การกำหนดจำนวนจุดทศนิยมให้ตัวเลข
- การแสดงผลข้อมูล
<?php //Auto refresh page $sec = "10"; header("Refresh: $sec; url=$page"); ?> <html> <head> </head> <body> <?php $servername = "localhost"; $username = "xxx"; $password = "xxx"; $dbname = "xxx"; // Create connection // Method 1 // $conn = new mysqli($servername, $username, $password, $dbname); // Method 2 $conn = mysqli_connect($servername, $username, $password, $dbname); mysqli_set_charset($conn, "utf8"); $chkcon = true; // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); $chkcon = false; } //Set SQL $sql = "SELECT * FROM tableName WHERE col1='xxx' "; //Run SQL $result = $conn->query($sql); $row = $result->fetch_assoc(); //Read data in column $A = row["col1"]; $B = row["col2"]; $C = row["col3"]; //Convert string to double $D = floatval(raw["col4"]); //Set number format 2 digits $E = number_format($D,2); //Close connection mysqli_close($conn); //Display data echo $A . " " . $B . " " . $C echo $D echo $E ?> </body> </html>