Wednesday, December 24, 2014

MySQL Database Connection using PDO

 $name = 'root'; # user-supplied data  
 try {  
 $conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);  
 #note: mysql:host=localhost(replace it by your hostname) , mysql:dbname:myDatabase (replace it by your database name), insert username and password for database at the end of this line  
 $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  
 $data = $conn->query('SELECT * FROM myTable WHERE name = ' . $conn->quote($name));  
 foreach($data as $row) {  
 print_r($row);  
 }  
 } catch(PDOException $e) {  
 echo 'ERROR: ' . $e->getMessage();  
 }  

No comments:

Post a Comment