Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- try {
- $db = new PDO('mysql:host=localhost;dbname=localdb;charset=utf8',
- 'user',
- 'pass');
- $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
- $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
- }
- catch(PDOException $ex) {
- echo "did not connect...";
- }
- $sth = $db->prepare("SELECT *FROM MyTable;");
- $sth->execute();
- Database=localdb;Data Source=127.0.0.1:54306;User Id=azure;Password=6#vWHD_$
- $dsn = 'mysql:dbname=localdb;host=127.0.0.1:54306;charset=utf8';
- $user = 'azure';
- $password = '6#vWHD_$';
- try {
- $dbh = new PDO($dsn, $user, $password);
- } catch (PDOException $e) {
- echo 'Connection failed: ' . $e->getMessage();
- exit;
- }
- echo "Success: A proper connection to MySQL was made!";
- $connectstr_dbhost = '';
- $connectstr_dbname = '';
- $connectstr_dbusername = '';
- $connectstr_dbpassword = '';
- foreach ($_SERVER as $key => $value) {
- if (strpos($key, "MYSQLCONNSTR_localdb") !== 0) {
- continue;
- }
- $connectstr_dbhost = preg_replace("/^.*Data Source=(.+?);.*$/", "\1", $value);
- $connectstr_dbname = preg_replace("/^.*Database=(.+?);.*$/", "\1", $value);
- $connectstr_dbusername = preg_replace("/^.*User Id=(.+?);.*$/", "\1", $value);
- $connectstr_dbpassword = preg_replace("/^.*Password=(.+?)$/", "\1", $value);
- }
- /*Add at the begining of the file
- if you want to connect to custom database then fill this variable $connectstr_dbname = 'MY_CUSTOM_DB'; and comment or remove $connectstr_dbname = preg_replace("/^.*Database=(.+?);.*$/", "\1", $value); from below code...
- */
- $connectstr_dbhost = '';
- $connectstr_dbname = '';
- $connectstr_dbusername = '';
- $connectstr_dbpassword = '';
- foreach ($_SERVER as $key => $value) {
- if (strpos($key, "MYSQLCONNSTR_localdb") !== 0) {
- continue;
- }
- $connectstr_dbhost = preg_replace("/^.*Data Source=(.+?);.*$/", "\1", $value);
- $connectstr_dbname = preg_replace("/^.*Database=(.+?);.*$/", "\1", $value);
- $connectstr_dbusername = preg_replace("/^.*User Id=(.+?);.*$/", "\1", $value);
- $connectstr_dbpassword = preg_replace("/^.*Password=(.+?)$/", "\1", $value);
- }
- // ** MySQL settings - You can get this info from your web host ** //
- /** The name of the database for WordPress */
- define('DB_NAME', $connectstr_dbname);
- /** MySQL database username */
- define('DB_USER', $connectstr_dbusername);
- /** MySQL database password */
- define('DB_PASSWORD', $connectstr_dbpassword);
- /** MySQL hostname : this contains the port number in this format host:port . Port is not 3306 when using this feature*/
- define('DB_HOST', $connectstr_dbhost);
Add Comment
Please, Sign In to add comment