Guest User

Untitled

a guest
Dec 26th, 2017
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. try {
  2. $db = new PDO('mysql:host=localhost;dbname=localdb;charset=utf8',
  3. 'user',
  4. 'pass');
  5. $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
  6. $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  7. }
  8. catch(PDOException $ex) {
  9. echo "did not connect...";
  10. }
  11.  
  12. $sth = $db->prepare("SELECT *FROM MyTable;");
  13. $sth->execute();
  14.  
  15. Database=localdb;Data Source=127.0.0.1:54306;User Id=azure;Password=6#vWHD_$
  16.  
  17. $dsn = 'mysql:dbname=localdb;host=127.0.0.1:54306;charset=utf8';
  18. $user = 'azure';
  19. $password = '6#vWHD_$';
  20.  
  21. try {
  22. $dbh = new PDO($dsn, $user, $password);
  23. } catch (PDOException $e) {
  24. echo 'Connection failed: ' . $e->getMessage();
  25. exit;
  26. }
  27.  
  28. echo "Success: A proper connection to MySQL was made!";
  29.  
  30. $connectstr_dbhost = '';
  31. $connectstr_dbname = '';
  32. $connectstr_dbusername = '';
  33. $connectstr_dbpassword = '';
  34.  
  35. foreach ($_SERVER as $key => $value) {
  36. if (strpos($key, "MYSQLCONNSTR_localdb") !== 0) {
  37. continue;
  38. }
  39.  
  40. $connectstr_dbhost = preg_replace("/^.*Data Source=(.+?);.*$/", "\1", $value);
  41. $connectstr_dbname = preg_replace("/^.*Database=(.+?);.*$/", "\1", $value);
  42. $connectstr_dbusername = preg_replace("/^.*User Id=(.+?);.*$/", "\1", $value);
  43. $connectstr_dbpassword = preg_replace("/^.*Password=(.+?)$/", "\1", $value);
  44. }
  45.  
  46. /*Add at the begining of the file
  47. 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...
  48. */
  49.  
  50. $connectstr_dbhost = '';
  51. $connectstr_dbname = '';
  52. $connectstr_dbusername = '';
  53. $connectstr_dbpassword = '';
  54.  
  55. foreach ($_SERVER as $key => $value) {
  56. if (strpos($key, "MYSQLCONNSTR_localdb") !== 0) {
  57. continue;
  58. }
  59.  
  60. $connectstr_dbhost = preg_replace("/^.*Data Source=(.+?);.*$/", "\1", $value);
  61. $connectstr_dbname = preg_replace("/^.*Database=(.+?);.*$/", "\1", $value);
  62. $connectstr_dbusername = preg_replace("/^.*User Id=(.+?);.*$/", "\1", $value);
  63. $connectstr_dbpassword = preg_replace("/^.*Password=(.+?)$/", "\1", $value);
  64. }
  65.  
  66. // ** MySQL settings - You can get this info from your web host ** //
  67. /** The name of the database for WordPress */
  68. define('DB_NAME', $connectstr_dbname);
  69.  
  70. /** MySQL database username */
  71. define('DB_USER', $connectstr_dbusername);
  72.  
  73. /** MySQL database password */
  74. define('DB_PASSWORD', $connectstr_dbpassword);
  75.  
  76. /** MySQL hostname : this contains the port number in this format host:port . Port is not 3306 when using this feature*/
  77. define('DB_HOST', $connectstr_dbhost);
Add Comment
Please, Sign In to add comment