Create MySQL database on PHP. OOP example open

Create MySQL database on PHP. OOP example

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators
Tested: PHP 5.6 +

To execute queries on the mysqli object, the query() method is called, to which the SQL command to be executed is passed:

$conn = new mysqli("localhost", "root", "password");
$conn->query(SQL command);

method query() returns true or false.

Create a database

The CREATE DATABASE command is used to create a database in MySQL

For example, lets create a testdb database:

$conn = new mysqli("localhost", "root", "password");

if($conn->connect_error){//check connection
    die("Error: " . $conn->connect_error);
}

// create  testdb database
$sql = "CREATE DATABASE testdb";

if($conn->query($sql)){
    echo "Database is created";
} else{
    echo "Database creating failed!" . $conn->error;
}

//stop the connection
$conn->close();
0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation