Database contains one or more tables.
You need CREATE privileges to create or delete MySQL database.
Create a MySQL database using MySQLi and PDO
CREATE DATABASE statement to create a database in MySQL.
In the following example, we create a database called "myDB" of:
Example (MySQLi - Object Oriented)
connect_error) {die ( "connection failed:" $ conn-> connect_error);.} // Create database $ sql = "CREATE DATABASE myDB"; if ($ conn-> query ($ sql) === TRUE) {echo "database successfully created";} else {echo "error creating database:" $ conn-> error;} $ conn-> close ();>.?
Note: When you create a new database, you must specify three parameters mysqli objects (servername, username and password).
Tip: If you use a different port (default is 3306), add an empty string for the database parameters, such as: new mysqli ( "localhost", "username", "password", "", port)
Examples of (MySQLi Procedural)
Note: The following myDBPDO create a database using the PDO instance:
Examples
Use PDO:
$servername = "localhost";
$username = "username";
$password = "password";
try {
$conn = new PDO("mysql:host=$servername", $username, $password);
// set the PDO error mode is abnormal
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "CREATE DATABASE myDBPDO";
// Use exec (), because there is no result is returned
$conn->exec($sql);
echo "Database successfully created
";
}
catch(PDOException $e)
{
echo $sql . "
" . $e->getMessage();
}
$conn = null;
?>
Tip: Use the PDO's biggest advantage is that you can use an exception class to handle problems when the database query process problems. If abnormal try {} block, the script execution stops and jumps to the first catch () {} block code execution. In the above code block, we captured the output of the SQL statement and generate an error message.
View all PHP tutorial article: https://www.codercto.com/courses/l/5.html (edit: Leilin Peng Source: network intrusion deleted)