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

后端 未结 7 1154
悲哀的现实
悲哀的现实 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:24

    Here's some same code.

    // ...Call the database connection settings
    require( path to /wp-config.php );
    
    // ...Connect to WP database
    $dbc = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
    if ( !$dbc ) {
        die( 'Not Connected: ' . mysql_error());
    }
    // Select the database
    $db = mysql_select_db(DB_NAME);
    if (!$db) {
        echo "There is no database: " . $db;
    }
    
    // ...Formulate the query
    $query = "
        SELECT *
        FROM `wp_posts`
        WHERE `post_status` = 'publish'
        AND `post_password` = ''
        AND `post_type` = 'post'
        ";
    
    // ...Perform the query
    $result = mysql_query( $query );
    
    // ...Check results of the query and terminate the script if invalid results
    if ( !$result ) {
        $message = '

    Invalid query.

    ' . "\n"; $message .= '

    Whole query: ' . $query ."

    \n"; die ( $message ); } // Init a variable for the number of rows of results $num_rows = mysql_num_rows( $result ); // Print the number of posts echo "$num_rows Posts"; // Free the resources associated with the result set if ( $result ) { mysql_free_result( $result ); mysql_close(); }

提交回复
热议问题