Cannot contact DB using MAMP

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";
}

Tyler

Posted 2018-11-05T03:25:24.903

Reputation: 43

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 – 2018-11-05T11:45:05.947

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 – 2018-11-05T17:46:32.503

No answers