如何从wp-config.php中读取值(PHP定义的常量)? [英] How do I read values (PHP defined constants) from wp-config.php?

查看:54
本文介绍了如何从wp-config.php中读取值(PHP定义的常量)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从 wp-config 文件中获取用户名,密码等,才能连接到自定义PDO数据库.

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

目前,我在另一个文件中拥有此信息,但我只想使用 wp-config .

Currently I have another file where I have this info, but I would like to only use the wp-config.

那么我如何读取 wp-config 的不同属性?

So how can I read the different properties of wp-config?

推荐答案

这里有一些相同的代码.

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 = '<p>Invalid query.</p>' . "\n";
    $message .= '<p>Whole query: ' . $query ."</p> \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();
}

这篇关于如何从wp-config.php中读取值(PHP定义的常量)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆