樹心幽徑

« 20200102修改lifetype BLOG的summary.php選單 | Main | 20200102用JavaScript設計二分搜尋法bs01.htm »

20200120設計可連線LAMP-MYSQL資料庫伺服器並新增資料庫、資料表、資料錄的PHP程式(c1.php)
2020/01/02,13:04

(一)確定LAMP已在WIN10安裝妥當並提供服務,安裝過程詳如:20191224在win10下載、安裝並使用Appserv9.3.0應用程式伺服程式

(二)設計可連線MYSQL並新增資料庫、資料表、資料錄的PHP程式:

(1)在本機用記事本編寫 如下內容並存在c:\appserv\www之中檔名為c1.php

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

$con = mysqli_connect('127.0.0.1', 'root', 'w??????', 'mysql');
if (!$con) { echo "Error"; exit(); }
$sql = 'drop DATABASE selldb;';
executeSQL($con,$sql);

$sql = 'CREATE DATABASE selldb default character set utf8;';
executeSQL($con,$sql);

$sql = 'use selldb;';
executeSQL($con,$sql);

$sql="CREATE TABLE custs (id char(10),name char(20),amount float,tel char(20))";
executeSQL($con,$sql);

$sql = 'set names utf8;';
executeSQL($con,$sql);

$sql="insert into custs(id,name,amount,tel) values ('10001','小狗',156,'08-233375');";
executeSQL($con,$sql);

$sql="insert into custs(id,name,amount,tel) values ('10002','小貓',256,'08-233375');";
executeSQL($con,$sql);

$sql="insert into custs(id,name,amount,tel) values ('10001','小草',1000,'08-233375');";
executeSQL($con,$sql);

$sql="insert into custs (id,name,amount,tel) values ('10002','天牛' ,3000,'03-933375');";
executeSQL($con,$sql);

$sql="insert into custs (id,name,amount,tel) values ('20002','天牛' ,3000,'03-933375');";
executeSQL($con,$sql);

$sql="insert into custs (id,name,amount,tel) values ('30002','天牛' ,3000,'03-933375');";
executeSQL($con,$sql);

$sql= "select id,name,amount,tel from custs";
if( $query= mysqli_query($con, $sql) ){ echo " $sql OK!<br> "; } else { echo " $sql Failure!<br> "; }

while ($row = mysqli_fetch_array($query)){
        echo  $row['id'] . ":" . $row['name'] . ":" . $row['amount'] . "<br>";
}
mysqli_close ($con);

?>

</body>

 

 

(2) 執行c1.php結果如下

[drop DATABASE selldb;] OK!
[CREATE DATABASE selldb default character set utf8;] OK!
[use selldb;] OK!
[CREATE TABLE custs (id char(10),name char(20),amount float,tel char(20))] OK!
[set names utf8;] OK!
[insert into custs(id,name,amount,tel) values ('10001','小狗',156,'08-233375');] OK!
[insert into custs(id,name,amount,tel) values ('10002','小貓',256,'08-233375');] OK!
[insert into custs(id,name,amount,tel) values ('10001','小草',1000,'08-233375');] OK!
[insert into custs (id,name,amount,tel) values ('10002','天牛' ,3000,'03-933375');] OK!
[insert into custs (id,name,amount,tel) values ('20002','天牛' ,3000,'03-933375');] OK!
[insert into custs (id,name,amount,tel) values ('30002','天牛' ,3000,'03-933375');] OK!
select id,name,amount,tel from custs OK!
10001:小狗:156
10002:小貓:256
10001:小草:1000
10002:天牛:3000
20002:天牛:3000
30002:天牛:3000

 

迴響

 
Accessible and Valid XHTML 1.0 Strict and CSS Powered by LifeType