Skip to main content

PHP Connection To MySQL

Screenshot
Body

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

Example syntax for MySQLi (Procedural)

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

Example syntax for PDO (PHP Data Objects)

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

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<br />
  2. $conn = new PDO("mysql:host=localhost;dbname=myDB", 'username', 'password');<br />
  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.

Comments

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