0

Im working on a custom web application. I have a local development environment using MAMP. Im trying to contact my DB and it fails every time so i tried running a custom DB test script which also fails. (If it means anything i had to change my MAMP port to 8889). When i use wordpress on mamp it has no problems so im not sure what is causing this. Any help is appreciated, thank you.

<?php

# Fill our vars and run on cli
# $ php -f db-connect-test.php

$dbname = 'localhost';
$dbuser = 'admin';
$dbpass = '********';
$dbhost = 'Gotcha-6';

$connect = mysql_connect($dbhost, $dbuser, $dbpass) or die("Unable to 
Connect to '$dbhost'");
mysql_select_db($dbname) or die("Could not open the db '$dbname'");
$test_query = "SHOW TABLES FROM $dbname";
$result = mysql_query($test_query);
$tblCnt = 0;
while($tbl = mysql_fetch_array($result)) {
  $tblCnt++;
  #echo $tbl[0]."<br />\n";
}
if (!$tblCnt) {
  echo "There are no tables<br />\n";
} else {
  echo "There are $tblCnt tables<br />\n";
}
  • First and foremost you need to stop using MySQL and switch to PDO. If your database server is now listening on port 8889 you need to change your connection string to include the port number on the host name. When using PDO it will be in the options as port=8889. In your code above it looks like you have the $dbname and $dbhost values swapped. Chances are pretty good that $dbhost should be localhost. – Dave Nov 5 '18 at 11:45
  • Thank you, I will look into PDO which looks like it will be a lot more flexible. I was able to solve the problem by rerolling my ports AND creating a new user with admin privileges for the DB in phpmyadmin. Neither on their own seemed to work for some reason. – Tyler Nov 5 '18 at 17:46

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.