[英]Cannot connect to MYSQL in MAMP using PHP
I just installed MAMP and have created a MYSQL database. 我刚刚安装了MAMP并创建了一个MYSQL数据库。 I can access it via PHPMYADMIN.
我可以通过PHPMYADMIN访问它。
In my php page I have this, pasted directly from the MAMP webstart page-- 在我的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
);
The resulting page stops at this point, won't print anything below these instructions. 此时结果页面停止,将不会打印这些说明之下的任何内容。
I've tried changing the port in the MAMP preferences. 我尝试在MAMP首选项中更改端口。 I also included or die("Could not connect");
我也包含或死亡(“无法连接”); after the first line, but still don't get any text after the link data in the page.
在第一行之后,但在页面中的链接数据之后仍然没有收到任何文本。
I checked online, and others with the problem at least see the die text. 我在线检查了一下,其他有问题的人至少看到了模具文字。 I don't get that.
我不明白
I haven't changed any passwords or data other than mess with the port number. 除了弄乱端口号外,我没有更改任何密码或数据。
Any help would be appreciated! 任何帮助,将不胜感激!
Please give the following a try, I have developed and tested it locally, functionality within has been documented to help you understand what is going on in every step. 请尝试以下操作,我已经在本地进行了开发和测试,其中的功能已记录在案,可帮助您了解每个步骤的进展。
/**
*
* 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.
If you have any issues with this please attempt to break it up into smaller pieces while reviewing the PDO documentation. 如果对此有任何疑问,请在阅读PDO文档时尝试将其分解为较小的部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.