doujieluo5875 2016-07-25 23:35
浏览 121

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

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!

  • 写回答

1条回答 默认 最新

  • dourao3960 2016-07-26 00:44
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算