简体   繁体   中英

Cannot connect to MYSQL in MAMP using PHP

I just installed MAMP and have created a MYSQL database. I can access it via PHPMYADMIN.

In my php page I have this, pasted directly from the MAMP webstart page--

$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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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