Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How can I connect to the database using the wp-config.php file ?

I'm trying to make a script more Wordpress friendly, and I need to connect to the database, but without installing the script as a plugin.

Basically I have on my script

$host = "xxxxxxxxxxx"; //database location
$user = "xxxxxxxxxxx"; //database username
$pass = "xxxxxxxxxxx"; //database password
$db_name = "xxxxxxxx"; //database name

//Database Connection
$link = mysql_connect($host, $user, $pass);
mysql_select_db($db_name);

The script can not be installed as a plugin (which can make things more easy), so I need to connect to the database using the existing wp-config.php on the installation... Any ideas ???

Thanks in advance

Edit and Clarification

1- I need to use wp-config.php as it is, no modifications. 2- The script will be located at www.example.com/script/ 3- It can't be done as a plugin since the core of the script require to be publicly accessed without any login screen jumping around. 4- My question basically is how to connect to the database using the wp-config.php file by modifying the script above.

share|improve this question
1  
Please explain why exactly it's not possible as plugin. –  kaiser May 4 '12 at 7:53
    
Because the script require to be publicly accessed, not on the admin side( it will not work on any folder like wp-content/plugins since a login screen may come across ). –  user983248 May 4 '12 at 10:28
    
I think you might want to edit your question to say what you want to do with your script. Pretty much anything is possible as a plug-in :) –  Stephen Harris May 4 '12 at 13:19
    
IPN validation for Paypal, See, it didn't work for me while doing it from the Plugins folder, but yes from a folder outside the whole Wordpress installation –  user983248 May 4 '12 at 14:38

2 Answers 2

up vote 3 down vote accepted

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/wp-config.php');
mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
share|improve this answer
1  
Which you stated that it currently doesn't work in your own question. Not going to downvote, but please make shure that your answer really works and shows what the OP makes wrong. Thanks! :) Btw: Welcome to WPSE and don't let that little push by me hold you back from answering other questions. Answering is always highly appreciated. –  kaiser May 4 '12 at 7:52
    
The connection to the database works. The problem in my question is abstracting the function that calls it out to an external file. The values I'm using here are defines set in wp-config.php which you use to set up Wordpress. Did you at least try it before assuming it doesn't work? –  Akamaozu May 4 '12 at 9:55
    
This is a bit off the original question –  user983248 May 4 '12 at 10:44
    
Have you actually tried it? I had the exact same problem you have (connecting to the database) and I solved it by using the defines set by wp-config (like you requested). The only variable you need is $db_name, since all the rest are already in the WP environment thanks to wp-config.php. As long as the Wordpress environment is loaded, you have total access to the defines. edit: Is your script outside the Wordpress environment? –  Akamaozu May 4 '12 at 10:50
    
Yes, please read my last edit, and thanks for taking the time –  user983248 May 4 '12 at 11:01

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... Thisi s preferrable method for doing you DB stuff inside WordPress as you do not have to open any additional database connections. Check out this article for additional info: http://wp.smashingmagazine.com/2011/09/21/interacting-with-the-wordpress-database/

share|improve this answer
    
When I remove the link from your answer, I got no information about what the actual solution would be, aside from a hint that $wpdb can perform basic database tasks. Would you please mind to improve your answer to show off some basic example? Thanks. –  kaiser Apr 5 at 2:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.