我們如何使用 PHP 指令碼建立新資料庫?


眾所周知,PHP 為我們提供了一個名為 mysql_query 的函式來建立一個新資料庫。

示例

為了說明這一點,我們在以下示例中藉助 PHP 指令碼建立一個名為“Tutorials”的資料庫 −

<html>
   <head>
      <title>Creating MySQL Database</title>
   </head>
   <body>
      <?php
         $dbhost = 'localhost:3036';
         $dbuser = 'root';
         $dbpass = 'rootpassword';
         $conn = mysql_connect($dbhost, $dbuser, $dbpass);
         
         if(! $conn ) {
            die('Could not connect: ' . mysql_error());
         }
         echo 'Connected successfully<br />';
         $sql = 'CREATE DATABASE TUTORIALS';
         $retval = mysql_query( $sql, $conn );
         
         if(! $retval ) {
            die('Could not create database: ' . mysql_error());
         }
         echo "Database TUTORIALS created successfully
";          mysql_close($conn);       ?>    </body> </html>

更新於: 2020 年 6 月 22 日

95 次瀏覽

開啟你的 職業生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.