我刚刚安装了MAMP并创建了一个MYSQL数据库。我可以通过PHPMYADMIN访问它。
在我的php页面中,我有这个,直接从MAMP webstart页面粘贴 -
$user = 'root';
$password = 'root';
$db = 'local_db';
$host = 'localhost';
$port = 3306;
$link = mysql_connect(
"$host:$port",
$user,
$password
);
$db_selected = mysql_select_db(
$db,
$link
);
此时结果页面停止,不会在这些说明下方打印任何内容。
我尝试更改MAMP首选项中的端口。我也包括或死(“无法连接”);在第一行之后,但在页面中的链接数据之后仍然没有得到任何文本。
我在网上查了一下,其他有问题的人至少看到了死文。我不明白。
除了弄乱端口号之外,我没有更改任何密码或数据。
任何帮助将不胜感激!
答案 0 :(得分:0)
请尝试以下内容,我已在本地开发和测试,内部功能已记录在案,以帮助您了解每一步中发生的事情。
/**
*
* Modern method of connecting to a MySQL database and keeping it simple.
*
* If you would like to learn more about PDO,
* please visit http://php.net/manual/en/book.pdo.php
*
*/
//Set up database connection constants, so they cannot be changed.
define('DBHOST','127.0.0.1'); //Change this to the ip address of your database
define('DBNAME','test'); // Change this to the database name you are trying to connect to.
define('DBUSER','databaseuser'); // Insure this user is not the root user!!!!
define('DBPASS','databasepassword'); // Insure this is not the root password!!!!
//Let's try to connect to the database first.
try {
//Initiate a new PDO object called $MYDB and pass it the proper information to make
//the connection
$MYDB = new PDO("mysql:host=".DBHOST.";dbname=".DBNAME."", DBUSER, DBPASS);
//If we are successful show it :D for the test page, if this is for production you should not show this.
echo "Database connection was successful.";
//If this does not worth catch the exception thrown by PDO so we can use it.
} catch(PDOException $e) {
//Show that there was an issue connecting to the database. Do not be specific because,
//user's do not need to know the specific error that is causing a problem for security
//reasons.
echo "Oh, sorry there was an issue with your request please try again.";
//Since we had an issue connecting to the database we should log it, so we can review it.
error_log("Database Error" . $e->getMessage());
}
//Since this is 100% php code we do not need to add a closing php tag
//Visit http://php.net/manual/en/language.basic-syntax.phptags.php for more information.
如果您对此有任何疑问,请在查看PDO文档时尝试将其分解为更小的部分。