PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, February 14, 2022

[FIXED] Can't establish connect to MySQL over localhost with MAMP

 February 14, 2022     mamp, mysql, php     No comments   

Issue

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);
?>


Answered By - hamdy
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing