亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

PHP MySQL creates database

The database has one or more tables.

You need CREATE permission to create or delete a MySQL database.

Creating a MySQL database using MySQLi and PDO

The CREATE DATABASE statement is used to create a database in MySQL.

In the following example, a database named "myDb" is created:

Instance (MySQLi - Object-oriented)

<?php
$servername = "localhost";
$username = "username";
$password ="password";
// 創(chuàng)建連接
$conn =new mysqli($servername, $username, $password);
// 檢測(cè)連接
if ($conn->connect_error) {
    die("連接失敗: " . $conn->connect_error);
} 
// 創(chuàng)建數(shù)據(jù)庫
$sql = "CREATE DATABASE myDB";
if ($conn->query($sql) === TRUE) {
    echo "數(shù)據(jù)庫創(chuàng)建成功";
} else {
    echo "Error creating database: " . $conn->error;
}
$conn->close();
?>

Note: When you create a new database, you must specify three parameters (servername, username and password) for the mysqli object.

Tip: If you use another port (default is 3306), add an empty string for the database parameters, such as: new mysqli("localhost", "username ", "password", " ", port)

About database naming:

1. It is not allowed to use Chinese characters as the name of the database (table name, field name Also)

2. Try not to use reserved words (keywords) in the database name

3. Do not use special symbols in the database name, but you can have multiple words separated by underscores

Instance (MySQLi Procedural)

<?php
$servername = "localhost";
$username = "username";
$password ="password";
// 創(chuàng)建連接
$conn = mysqli_connect($servername,$username, $password);
// 檢測(cè)連接
if (!$conn) {
    die("連接失敗: " . mysqli_connect_error());
}
// 創(chuàng)建數(shù)據(jù)庫
$sql = "CREATE DATABASE myDB";
if (mysqli_query($conn, $sql)) {
    echo "數(shù)據(jù)庫創(chuàng)建成功";
} else {
    echo "Error creating database: " . mysqli_error($conn);
}
mysqli_close($conn);
?>

Note: The following uses the PDO instance to create the database "myDBPDO":

Example

Using PDO:

<?php
$servername  = "localhost" ;
$username =  "username";
$password =  "password";
try {
    $conn  = new PDO ("mysql:host=$servername;dbname=myDB", $username, $password);
     // 設(shè)置 PDO 錯(cuò)誤模式為異常
     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     $sql = "CREATE DATABASE myDBPDO" ;
    // 使用 exec() ,因?yàn)闆]有結(jié)果返回
     $conn->exec($sql);
    echo  "數(shù)據(jù)庫創(chuàng)建成功<br>" ;
}
catch(PDOException $e)
{
    echo $sql . "<br>" . $e-> getMessage();
}
$conn = null;
?>

Tips: The biggest benefit of using PDO is that you can use the exception class when problems occur during the database query process to deal with the problem. If an exception occurs in the try{ } code block, the script will stop execution and jump to the first catch(){ } code block to execute the code. In the code block captured above we output the SQL statement and generate the error message.

PDO::setAttribute — Set an attribute. For details, please refer to: PDO::setAttribute

Using phpMyAdmin

can be executed by opening phpMyAdmin and directly entering the code and clicking the "Execute" button in the lower right corner. Operation:

localhost.png

Create database

CREATE DATABASE db_name;

View database

Create After creating the new database, you can view the newly created database through the following code:

SHOW CREATE DATABASE db_name;

Select database

When operating on a database, you need to select a database.

mysql_select_db() Used to select a database. If successful, the function returns true, if failed, returns false.

For detailed usage, please refer to: mysqli_select_db() function.


Continuing Learning
||
<?php $servername = "localhost"; $username = "username"; $password = "password"; // 創(chuàng)建連接 $conn = new mysqli($servername, $username, $password); // 檢測(cè)連接 if ($conn->connect_error) { die("連接失敗: " . $conn->connect_error); } // 創(chuàng)建數(shù)據(jù)庫 $sql = "CREATE DATABASE myDB"; if ($conn->query($sql) === TRUE) { echo "數(shù)據(jù)庫創(chuàng)建成功"; } else { echo "Error creating database: " . $conn->error; } $conn->close(); ?>
submitReset Code