how to get a file path of wp-config.php of wordpress site?

I am developing a plugin in which i need to include config and query the wordpress database on click ..My question is How to get url of wp-config.php dynamically ..I have used

include_once $_SERVER['DOCUMENT_ROOT'] . '/wp-config.php';

but i am getting error

include_once(): Failed opening /wp-config.php' for inclusion (include_path='.:/usr/share/php:/usr/share/pear')

and i have tried many options like

require_once('../../../wp-config.php');
require_once ("../wp-config.php");

I dunno wat is exact path ..

Any help is appreciated ..

Answers


In short, you shouldn't be trying to include wp-config.php from your plugin. There are few if any good reasons you would ever take this approach. Purists would go as far as to say it should never be done.

A WordPress plugin already has the proper context to access the global $wpdb variable for querying. Take a look at this example:

global $wpdb;

$results = $wpdb->get_results( $wpdb->prepare("SELECT * FROM {$wpdb->prefix}your_table WHERE column = %s", $your_value) );

Suggested reading:


Need Your Help

PHP Assistance with empty() in a loop

php loops foreach

Let's say I wanted to check to see if a variable is empty and then do something... I can do this:

Java - creating new file, how do I specify the directory with a method?

java file io directory

I know how to write a file to a specified directory by doing this:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.