Created
December 15, 2014 15:18
-
-
Save johnmorris/6f5d2ad14a7b81096588 to your computer and use it in GitHub Desktop.
Connect to a MySQL database with PHP using PDO and MySQLi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Be sure to input YOUR database details below | |
// Watch the associated videos here: https://www.youtube.com/playlist?list=PLLs69n7Q4dCx5_7ZwnxTymH8X0iRP_2vw | |
$db_user = 'DB USERNAME HERE'; | |
$db_pass = 'DB PASSWORD HERE'; | |
$db_name = 'DB NAME HERE'; | |
$db_host = 'localhost'; | |
//MySQLi | |
$mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name); | |
if ($mysqli->connect_errno) { | |
printf("Connect failed: %s\n", $mysqli->connect_error); | |
exit(); | |
} | |
$result->close(); | |
// PDO | |
//try { | |
// $conn = new PDO("mysql:host={$db_host};dbname={$db_name}", $db_user, $db_pass); | |
// $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
//} catch(PDOException $e) { | |
// echo 'ERROR: ' . $e->getMessage(); | |
//} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment