All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Create Database In MySQL With Connection Object Conn

Last Updated : Mar 11, 2024

How To Create Database In MySQL With Connection Object Conn

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 " ;
?>
  1. We write <?php tag to write PHP within it.
  2. Creating a connection with $con with mysqli() function with hostname, user name, password, and database name
  3. To check the connection we use connect_error statement. And echo to display the error message
  4. Then using echo to display a message that the server is successfully connected.
  5. ?> 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 " ;
?>
  1. We write <?php tag to write PHP within it.
  2. Declaring variables for $serverName, $userName, $password, $dbName.
  3. Creating a connection with $con with mysqli() function
  4. To check the connection we use the connect_error statement. And echo to display the error message
  5. Then use echo to display a message that the server is successfully connected.
  6. ?> 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 () ;
}
?>
  1. We write <?php tag to write PHP within it.
  2. Declaring variables for $serverName, $userName, $password, $dbName.
  3. Creating a connection with try using $conn with new PDO () as the MySQL host as $servername, dbName as $dbName, $userName, and $password
  4. $con to setAtrributes(), using echo to display a message that the server is successfully connected.
  5. Now use catch() and echo to display an error message.
  6. ?> 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.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪