樹心幽徑

« 20181218mysql資料庫連線查詢網頁程式 a1.php (用來列出selldb資料庫中custs資料表的第1筆資料) | Main | 20181220設計PHP函數dumpTable,用來列出selldb資料庫的custs資料表的全部資料錄 »

20181219建立selldb資料庫、建立custs資料表、加入三筆資料錄至custs中、列出custs中的資料錄的PHP程式(m2.php)。
2018/12/19,20:39

(1) code:

<body bgcolor=yellow>
<?php
$con = @mysqli_connect('localhost', 'root', 'w1????', 'mysql');
if (!$con) {
    echo "Error: " . mysqli_connect_error();
    exit();
}
echo 'Connected to MySQL OK!<hr>';

$sql = 'drop DATABASE IF EXISTS selldb;';
if( $query= mysqli_query($con, $sql) ){ echo " $sql OK!<br> "; } else { echo " $sql Failure!<br> "; }


$sql = 'CREATE DATABASE selldb default character set utf8;';
if( $query= mysqli_query($con, $sql) ){ echo " $sql OK!<br> "; } else { echo " $sql Failure!<br> "; }

$sql = 'use selldb;';
if( $query= mysqli_query($con, $sql) ){ echo " $sql OK!<br> "; } else { echo " $sql Failure!<br> "; }

$sql    = 'DROP TABLE IF EXISTS custs';
if( $query= mysqli_query($con, $sql) ){ echo " $sql OK!<br> "; } else { echo " $sql Failure!<br> "; }

$sql= "CREATE TABLE custs (id varchar(10) , name varchar(20), amount float default 0, tel   varchar(20)  default 'none' )" ;
if( $query= mysqli_query($con, $sql) ){ echo " $sql OK!<br> "; } else { echo " $sql Failure!<br> "; }

$sql = 'set names utf8;';
if( $query= mysqli_query($con, $sql) ){ echo " $sql OK!<br> "; } else { echo " $sql Failure!<br> "; }

$sql="insert into custs (id,name,amount,tel)  values ('10001','小草' ,1000,'08-233375');";
if( $query= mysqli_query($con, $sql) ){ echo " $sql OK!<br> "; } else { echo " $sql Failure!<br> "; }

$sql="insert into custs (id,name,amount,tel)  values ('10002','天牛' ,3000,'03-933375');";
if( $query= mysqli_query($con, $sql) ){ echo " $sql OK!<br> "; } else { echo " $sql Failure!<br> "; }

$sql="insert into custs (id,name,amount,tel)  values ('10003','地虎' ,5000,'02-444375');";
if( $query= mysqli_query($con, $sql) ){ echo " $sql OK!<br> "; } else { echo " $sql Failure!<br> "; }

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

echo "資料查詢如下<br>";
while ($row = mysqli_fetch_array($query)){
        echo  $row['id'] . ":" . $row['name'] . ":" . $row['amount'] . ":" . $row['tel'] . "<br>";
}

mysqli_close ($con);

?>
</body>

(2)result:

Connected to MySQL OK!


drop DATABASE IF EXISTS selldb; OK!
CREATE DATABASE selldb default character set utf8; OK!
use selldb; OK!
DROP TABLE IF EXISTS custs OK!
CREATE TABLE custs (id varchar(10) , name varchar(20), amount float default 0, tel varchar(20) default 'none' ) OK!
set names utf8; 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 ('10003','地虎' ,5000,'02-444375'); OK!
select id,name,amount,tel from custs OK!
資料查詢如下
10001:小草:1000:08-233375
10002:天牛:3000:03-933375
10003:地虎:5000:02-444375

迴響

 
Accessible and Valid XHTML 1.0 Strict and CSS Powered by LifeType