Last active
June 6, 2018 06:38
-
-
Save mindevolution/412c4920dd3acf745a2e188c483cb1ac to your computer and use it in GitHub Desktop.
Test php mysql db connection
This file contains 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 | |
$servername = '127.0.0.1'; | |
$username = 'dbuser'; | |
$password = 'dbpassword'; | |
$dbname = 'dbname'; | |
// Create connection | |
$conn = new mysqli($servername, $username, $password); | |
// Check connection | |
if ($conn->connect_error) { | |
die("Connection failed: " . $conn->connect_error); | |
} | |
echo "Connected successfully"; | |
echo "\nTest PDO\n"; | |
try { | |
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); | |
// set the PDO error mode to exception | |
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
echo "PDO Connected successfully"; | |
} catch(PDOException $e) { | |
echo "PDO Connection failed: " . $e->getMessage(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment