简体   繁体   English

无法使用MAMP通过localhost建立与MySQL的连接

[英]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. 我一直在尝试设置MAMP(普通版)并使用PHP连接到MySQL数据库,但似乎无法正确连接。 It's running on localhost, the user 'test' is on localhost, and has the privileges of SELECT, INSERT, UPDATE, and DELETE. 它在localhost上运行,用户“ test”在localhost上,并且具有SELECT,INSERT,UPDATE和DELETE特权。 Apache is using port 80, and MySQL 3306. Where have I gone wrong? Apache使用端口80和MySQL3306。我哪里出错了? Oh, and yes, I've tried using 127.0.0.1 but to no avial. 哦,是的,我尝试使用127.0.0.1,但没有帮助。

<?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. 编辑:我已经验证我可以从终端连接到MySQL。 /Applications/MAMP/Library/bin/mysql --host=localhost -uroot -proot / Applications / MAMP / Library / bin / mysql --host = localhost -uroot -proot

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

mysql_query is not in use anymore, change all your mysql_ 不再使用mysql_query,更改所有mysql_

use mysqli_query($connection, "Query") or PDO statements 使用mysqli_query($ connection,“ Query”)或PDO语句

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM