简体   繁体   English

无法使用MAMP连接到数据库

[英]Can't connect to a database using MAMP

I am trying to connect to a database with the name auth using PHP. 我正在尝试使用PHP连接到名称为auth的数据库。 I made the database with SequelPro and I am connecting using MAMP My PHP code is 我用SequelPro创建了数据库,并且正在使用MAMP连接我的PHP代码是

$server = 'localhost';
$username = 'root';
$password = 'root';
$database = 'auth';

try {
    $conn = new PDO("mysql:host=$server;dbname=$database; $username, $password");
} catch(PDOException $e) {
    die("Connection failed ". $e->getMessage());

Your quote usage for the connection is incorrect and makes it so only 1 parameter is being sent to the PDO connection. 您对连接的引用用法不正确,并使其无效,因此仅1个参数被发送到PDO连接。 Correct code: 正确的代码:

$conn = new PDO("mysql:host=$server;dbname=$database;", $username, $password);

Simple demo ( https://eval.in/647408 ): 简单演示( https://eval.in/647408 ):

function simple($a, $b = 'empty', $c = 'empty') {
     var_dump('a=' . $a);
     var_dump('b=' . $b);
     var_dump('c=' . $c);
}
simple('a,b,c');
echo "\n" . 'version 2' . "\n\n";
simple('a','b','c');

Output: 输出:

string(7) "a=a,b,c"
string(7) "b=empty"
string(7) "c=empty"

version 2

string(3) "a=a"
string(3) "b=b"
string(3) "c=c"

With the commas in quotes they aren't read as parameter delimiters. 引号中的逗号不会被视为参数定界符。

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

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