How do I read values (PHP defined constants) from wp-config.php?

后端 未结 7 1039
悲哀的现实
悲哀的现实 2021-02-07 03:40

I need to get username, password etc from the wp-config file to connect to a custom PDO database.

Currently I have another file where I have this info, but

7条回答
  •  梦如初夏
    2021-02-07 04:21

    If you want to connect to DB, for current versions of PHP, using mysqli extention is recommended (mysql extention is going to deprecate):

    require_once ("../wp-config.php"); // path to wp-config depends on your file locatoin
    $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    if ($mysqli->connect_errno) {
        echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
    }
    

提交回复
热议问题