In this Complete Guide About Create PHP With MySQL Connection, we discuss how PHP MySQL connection works using an example and also talk about how we use PHP functions with MySQL for database manipulation operations.
Here are the complete list what are we discuss on this article,
- Introduction to PHP With MySQL
- Create MySQL Connection Using PHP
- Close Connection Using PHP MySQL Function
- Example of PHP With MySQL Connection
- Useful PHP MySQL Functions
Introduction to PHP With MySQL Connection
As you know PHP is a server-side language and used to create dynamic web pages, on the other hand, MySQL is an open-source relational database management system.
At the time of web development, we use MySQL as DB to store data, and PHP used to get data to show on the web page.
Create MySQL Connection in PHP
1 2 3 4 5 6 7 8 9 10 11 12 |
$dbhost = "localhost"; $dbuser = "your_db_username"; $dbpass = "your_db_password"; $dbname = "database_name"; // Creating connection $connection = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname); // Checking connection if (!$connection) { die("Connection failed: " . mysqli_connect_error()); } |
Note:
mysql
andmysqli
both are used for database connection but on the newer PHP version or above PHP 5.6 version, PHP only supportsmysqli
not MySQL. MySQLi also called an improved version of MySQL.
On the above code we do Create MySQL connection,
- Declare variables like host, DB username, DB password, and DB name which are used for connection to DB.
- Then those variables passed as parameters to
mysqli_connect()
function. - We check the connectivity by using the
if
condition if any error occurs thenmysqli_connect_error()
this function shows an error message.
Close MySQL Database Connection in PHP
1 2 |
// closing connection mysqli_close($connection); |
Above is a syntax of close connection after complete the process or database use on the program.
Example of PHP With MySQL Connection
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
$dbhost = "localhost"; $dbuser = "your_db_username"; $dbpass = "your_db_password"; $dbname = "database_name"; // Creating connection $connection = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname); // Checking connection if (!$connection) { die("Connection failed: " . mysqli_connect_error()); } // Creating a database named testDB $sql = "CREATE DATABASE testDB"; if (mysqli_query($connection, $sql)) { echo "Database created successfully with the name testDB"; } else { echo "Error creating database: " . mysqli_error($connection); } // closing connection mysqli_close($connection); |
Code Highlights:
- At first, we establish MySQL database connection using
mysqli_connect()
function. - Then we check the connection or not and if not we show an error using
mysqli_error($connection)
function. - Now we write code for creating DB using
mysqli_query($connection, $sql)
function. - At last, we close the database connection using
mysqli_close($connection)
function.
7 Useful PHP MySQL Functions
- mysql_escape_string — Escapes a string for use in a mysql_query
- mysql_fetch_array — Fetch a result row as an associative array, a numeric array, or both
- mysql_fetch_assoc — Fetch a result row as an associative array
- mysql_query — Send a MySQL query
- mysql_real_escape_string — Escapes special characters in a string for use in an SQL statement
- mysql_result — Get result data
- mysql_select_db — Select a MySQL database
Here is a Complete Guide About PHP to MySQL connection and useful functions which are majorly used at the time of development.
To know more about PHP MySQL functions https://www.php.net/manual/en/ref.mysql.php
Also Check:
- mysql_fetch_row() VS mysql_fetch_object() VS mysql_fetch_assoc() VS mysql_fetch_array()
- Facts You want to Know About Use Of Mysqli_num_rows() Function
- Here’s What People Are Saying About Mysql Database Optimization For Better Performance
- Simple PHP MySQL CRUD Operation
Happy Coding..!
[…] Complete Guide About Create PHP With MySQL Connection […]
[…] Complete Guide About Create PHP With MySQL Connection […]
[…] Also Read: Complete Guide About Create PHP With MySQL Connection […]
[…] Also Read: Complete Guide About Create PHP With MySQL Connection […]
[…] Complete Guide About Create PHP With MySQL Connection […]
[…] Complete Guide About Create PHP With MySQL Connection […]