无法使用PHP连接到MAMP中的MYSQL



我刚刚安装了MAMP,并创建了一个MYSQL数据库。我可以通过PHPMYADMIN访问它。

在我的php页面中,我有这个,直接从MAMP Web启动页面粘贴而来——

$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文档时尝试将其分解为更小的部分。

最新更新