Skip to content

Instantly share code, notes, and snippets.

@martin-mok
Created August 14, 2019 01:35
Show Gist options
  • Save martin-mok/4fd88dc5ed0ec0fadb001af88169c3f0 to your computer and use it in GitHub Desktop.
Save martin-mok/4fd88dc5ed0ec0fadb001af88169c3f0 to your computer and use it in GitHub Desktop.
php code to connect mysql wtih different port
<?php
//for default MySQL port 3306
//$servername = "127.0.0.1";
//for MySQL deployed on port 3308
$servername = "127.0.0.1:3308";
$username = "root";
$password = "";
try {
$pdo = new PDO("mysql:host=$servername;", $username, $password);
// set the PDO error mode to exception
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
echo $pdo->query('select version()')->fetchColumn();
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment