Connect MySQL using Object Oriented way in PHP

Connecting MySQL database with PHP using Object Oriented way.

Source code :

<?php
$server = "localhost";
$username = "root";
$password = "";
//setting up the connection
$conn = new mysqli($server, $username, $password);
//checking the connection.
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected by Object Oriented way successfully";



Output :

Connected by Object Oriented way successfully

Notes :

To run the script:

1.Install any local host server and start it.
2.Type the above code in the installed directory

  • If it is WAMP,place the file in WWW folder (C:wamp64www).
  • If it is XAMPP,place the file in HTDocs folder (C:xampphtdocs).

and save it as php file.
3.In Browser,type localhost/filename.php and hit enter.

Note: The above code is not much differed from the procedural syntax whereas the PHP Data Objects (PDO) extension, a consistent interface for accessing databases requires the new Object Oriented method. It does not have the procedural interface.


Output :


                

Comments :