Skip to main content

PHP Connection To MySQL


PHP Connection To MySQL

If you want to know on How To Create PHP Connection To MySQL then you are at the right place. Which one do you prefer? MySQLi or PDO? In my opinion, it would be whatever you like to use it. MySQli and PDO have their advantages.

We have 3 ways to create this in PHP and MySQL. These are:

  • MySQLi (object-oriented)
  • MySQLi (procedural)
  • PDO (PHP Data Objects)

Creating Connection To MySQL

Example syntax for MySQLi (Object-Oriented)

  1. <?php
  2. $servername = "localhost";
  3. $username = "username";
  4. $password = "password";
  5.  
  6. // Create connection
  7. $conn = new mysqli($servername, $username, $password);
  8.  
  9. // Check connection
  10. if ($conn->connect_error) {
  11. die("Connection Error. Check your syntax." . $conn->connect_error);
  12. }
  13. echo "Successfully Connected!!!";
  14. ?>

Example syntax for MySQLi (Procedural)

  1. <?php
  2. $servername = "localhost";
  3. $username = "username";
  4. $password = "password";
  5.  
  6. // Create connection
  7. $conn = mysqli_connect($servername, $username, $password);
  8.  
  9. // Check connection
  10. if (!$conn) {
  11. die("Connection Error. Check your syntax." . mysqli_connect_error());
  12. }
  13. echo "Successfully Connected!!!";
  14. ?>

Example syntax for PDO (PHP Data Objects)

  1. <?php
  2. $servername = "localhost";
  3. $username = "username";
  4. $password = "password";
  5.  
  6. try {
  7. $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
  8. // set the PDO error mode to exception
  9. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  10. echo "Successfully Connected!!!";
  11. }
  12. catch(PDOException $e)
  13. {
  14. echo "Connection Error. Check your syntax." . $e->getMessage();
  15. }
  16. ?>

To Close The Connection To MySQL

Syntax

MySQLi (Object-Oriented)
$conn->close();
MySQLi (Procedural)
mysqli_close($conn);
PDO (PHP Data Objects)
$conn = null;

But if you ask me which I prefer to use, my answer would be PDO (PHP Data Objects).

I have a syntax to use this to have a connection to MySQL. And to make it simple and short, and easy to remember. This is the syntax:

  1. <?php
  2. $conn = new PDO("mysql:host=localhost;dbname=myDB", 'username', 'password');
  3. ?>

Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Submitted byaishnnaon Fri, 05/04/2018 - 09:00

php is used to develop the web application. php and mysql is used as a database. mysql is used in php to develop a web application. php is designed tool.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.