Search code examples
phpmysqlmamp

Can't establish connect to MySQL over localhost with MAMP


I've been attempting to set up MAMP (normal edition) and connect to the MySQL database using PHP, but I can't seem to get the connection right. It's running on localhost, the user 'test' is on localhost, and has the privileges of SELECT, INSERT, UPDATE, and DELETE. Apache is using port 80, and MySQL 3306. Where have I gone wrong? Oh, and yes, I've tried using 127.0.0.1 but to no avial.

<?php

$link = mysql_connect("localhost:3306", “test”, “development”) or die("Could not connect");
mysql_select_db(“login_test”) or die("Could not select database");

$arr = array();

$rs = mysql_query("SELECT * FROM users");
while($obj = mysql_fetch_object($rs)) {
    $arr[] = $obj;
}

echo json_encode($arr);

?>

EDIT: I have verified I can connect to MySQL from the terminal. /Applications/MAMP/Library/bin/mysql --host=localhost -uroot -proot


Solution

  • replace “ quotes with " i run your code successfully but i changed “ by "

     <?php
    
    $link = mysql_connect("localhost:3306", "root", "") or die("Could not connect");
    mysql_select_db("login_test") or die("Could not select database");
    
    $arr = array();
    
    $rs = mysql_query("SELECT * FROM users");
    while($obj = mysql_fetch_object($rs)) {
    $arr[] = $obj;
    }
    
    echo var_dump($arr);
    ?>