PHP 產生資料表

  1. 切換到網頁根目錄。
    [root@kvm3 ~]# cd /var/www/html/
    
  2. 撰寫 createtbl.php,在 dywphp 資料庫產生資料表 tblphp。
    [root@kvm3 html]# vim createtbl.php
    [root@kvm3 html]#
    <?php
    $dbhost = 'localhost:3306';
    $dbuser = 'root';
    $dbpass = '123qwe';
    $conn = mysqli_connect($dbhost, $dbuser, $dbpass) 
    	or die(mysqli_connect_error().PHP_EOL);
    echo 'Connected successfully'.PHP_EOL;
    $sql = "CREATE TABLE tblphp( ".
           "id INT NOT NULL AUTO_INCREMENT, ".
           "title VARCHAR(100) NOT NULL, ".
           "author VARCHAR(40) NOT NULL, ".
           "submission_date DATE, ".
           "PRIMARY KEY ( id )); ";
    mysqli_select_db( $conn, 'dywphp' )
    	or die('Error: '.mysqli_error($conn).PHP_EOL);
    mysqli_query( $conn, $sql )
    	or die('Error: '.mysqli_error($conn).PHP_EOL);
    echo "Table tblphp created successfully\n";
    mysqli_close($conn); 
    ?>
    
  3. 以 curl 連線 createtbl.php,成功在資料庫 dywphp 產生資料表 tblphp。
    [root@kvm3 html]# curl -s http://127.0.0.1/createtbl.php
    Connected successfully
    Table tblphp created successfully
    
  4. 再以 curl 連線 createtbl.php,回應資料表 tblphp 已存在。
    [root@kvm3 html]# curl -s http://127.0.0.1/createtbl.php
    Connected successfully
    Error: Table 'tblphp' already exists
    
  5. 查看 dywphp 資料庫的資料表,出現 tblphp。
    [root@kvm3 html]# mariadb -uroot -p123qwe -e "use dywphp; show tables"
    +------------------+
    | Tables_in_dywphp |
    +------------------+
    | tblphp           |
    +------------------+