[英]Cannot connect to MYSQL in MAMP using PHP
我剛剛安裝了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首選項中更改端口。 我也包含或死亡(“無法連接”); 在第一行之后,但在頁面中的鏈接數據之后仍然沒有收到任何文本。
我在線檢查了一下,其他有問題的人至少看到了模具文字。 我不明白
除了弄亂端口號外,我沒有更改任何密碼或數據。
任何幫助,將不勝感激!
請嘗試以下操作,我已經在本地進行了開發和測試,其中的功能已記錄在案,可幫助您了解每個步驟的進展。
/**
*
* 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文檔時嘗試將其分解為較小的部分。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.