In this tutorial we are going to learn connecting to MySQL with PHP using PDO.
“PDO – PHP Data Objects – is a database access layer providing a uniform method of access to multiple databases.”
You don’t need to worry about different syntax for different database types, PDO will do this painless.
# connect to the database try { $dbh = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass); $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); // this will enable printing the exceptions # It's a select statement $statement = $dbh->prepare('SELECT * FROM FROM users'); $statement->execute(); $result = $statement->fetchAll(); } catch(PDOException $e) { echo "Oops something gone wrong!"; }
Did you notice the setAttribute function and the params, this will enable the error more on in your database connection,
Leave a Reply