How to make database connection in php?



How to make database connection in php?..

Answer / Mahesh Chauhan

To connect to a database in PHP, you can use the PDO (PHP Data Objects) library. Here is an example:nn<?php try {n $db = new PDO('mysql:host=localhost;dbname=testdb', 'username', 'password');n $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);n echo 'Connected successfully';n} catch(PDOException $e) {necho 'Connection failed: ' . $e->getMessage();n}n?>

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More PHP Interview Questions

What is empty php?

1 Answers  


Is array function in php?

1 Answers  


What is session data?

1 Answers  


Explain a resource?

1 Answers  


Explain the types of functions for Splitting String?

1 Answers  


Is session a cookie?

1 Answers  


Is php 5 still supported?

1 Answers  


Where are php configuration settings stored?

1 Answers  


What is difference between $x and $$x

5 Answers   Mindex, Net Solution, Procon IT Solutions, Trigent,


What is difference Between PHP 5 and 7?

1 Answers  


How cookies are transported from browsers to servers?

1 Answers  


How is a constant defined in a PHP script?

1 Answers  


Categories