Skip to content

Instantly share code, notes, and snippets.

@mindevolution
Last active June 6, 2018 06:38
Show Gist options
  • Save mindevolution/412c4920dd3acf745a2e188c483cb1ac to your computer and use it in GitHub Desktop.
Save mindevolution/412c4920dd3acf745a2e188c483cb1ac to your computer and use it in GitHub Desktop.
Test php mysql db connection
<?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