Wordpress - Connect to database using wordpress wp-config file
Using the defines the user sets in wp-config:
mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
EDIT: Since your script is outside the Wordpress environment, what you want to do is initiate it before using the defines in wp-config.
require_once('./path/to/the/wp-config.php');
mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
You can make your script a part of your WordPress post, just use the $wpdb
object provided by the WordPress itself. The $wpdb
object already has the database connection established and you can use it to perform any database operation: insert, update, query etc... This is preferable method for doing you DB stuff inside WordPress as you do not have to open any additional database connections.
Here is a simple example for getting the future posts for instance:
$posts = $wpdb->get_results("SELECT ID, post_title FROM wp_posts WHERE post_status = 'future' AND post_type='post' ORDER BY post_date ASC LIMIT 0,4");
Check out this article for additional info: http://wp.smashingmagazine.com/2011/09/21/interacting-with-the-wordpress-database/