PHP Connection To MySQL

Submitted by: 
Language: 
Visitors have accessed this post 3274 times.

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.


Comments

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.php training

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.