How To Create Database In MySQL With Connection Object Conn
Last Updated : Mar 11, 2024
IN - PHP MySQL | Written & Updated By - Riya

In this article we will show you the solution of how to create database in MySQL with connection object conn, now we have to create a database first on the Localhost server.
After adding some data there we can connect the MySQL server by using php.
In this tutorial, we will see two different methods here:
Mysqli() method: this function is used to connect the MySql server.
Syntax
$con = new mysqli (host, username, password, dbname)
PDO class method: PHP Document Object or PDO is a framework that is used to access databases in PHP.
Step By Step Guide On How To Create Database In MySQL With Connection Object Conn :-
Method 1
we will see how to create a database in MySQL with connection object conn by using Mysqli () function in both of the examples below.
Example 1
In this example, we directly add values in Mysqli () function. Let us see the code first.
<?php
//create a connection
$conn = new mysqli ("localhost","root", "", "test") ;
//check the connection
if ($conn -> connect_error) {
die (" Connection failed ") ;
}
echo " Connected Successfully " ;
?>
- We write <?php tag to write PHP within it.
- Creating a connection with $con with mysqli() function with hostname, user name, password, and database name
- To check the connection we use connect_error statement. And echo to display the error message
- Then using echo to display a message that the server is successfully connected.
- ?> to close the php code.
Example 2
In this example, we added the data by creating variables.
<?php
$db_host = "localhost" ;
$db_user = "root" ;
$db_password = "" ;
$db_name = "test" ;
//create connection
$conn = new mysqli ($db_host,$db_user, $db_password, $db_name) ;
//check connection
if ($conn -> connect_error) {
die (" Connection failed ") ;
}
echo " Connected Successfully " ;
?>
- We write <?php tag to write PHP within it.
- Declaring variables for $serverName, $userName, $password, $dbName.
- Creating a connection with $con with mysqli() function
- To check the connection we use the connect_error statement. And echo to display the error message
- Then use echo to display a message that the server is successfully connected.
- ?> to close the php code.
Method 2
In the example below, we are going to see the PDO class method.
<?php
$serverName = "localhost" ;
$userName = "root" ;
$password = "" ;
$dbName = "test" ;
//creating connection
try {
$conn = new PDO("mysql:host = $serverName; dbname = $dbName", $userName, $password) ;
$conn -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION) ;
echo "Connection Success!" ;
}
//checking connection
catch (PDOException $e) {
echo "Error in connection". $e -> getMessage () ;
}
?>
- We write <?php tag to write PHP within it.
- Declaring variables for $serverName, $userName, $password, $dbName.
- Creating a connection with try using $conn with new PDO () as the MySQL host as $servername, dbName as $dbName, $userName, and $password
- $con to setAtrributes(), using echo to display a message that the server is successfully connected.
- Now use catch() and echo to display an error message.
- ?> to close the php code.
Conclusion :-
At last, here in conclusion, here we can say that with this article’s help, we know how to create a database in MySQL with connection object conn in php.
I hope this article on how to create database in MySQL with connection object conn helps you and the steps and method mentioned above are easy to follow and implement.













