0

I have used the mysql_connect() to connect to the wordpress DB but am getting the error as...

/** The name of the database for WordPress */

define('DB_NAME', '---my DB name---');



/** MySQL database username */

define('DB_USER', '---my user name----');



/** MySQL database password */

define('DB_PASSWORD', '---my password---');



/** MySQL hostname */

define('DB_HOST', '----host name----');

mysql_connect(DB_HOST,DB_USER,DBPASSWORD,DB_NAME);

Error: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'

Till Helge
  • 9,253
  • 2
  • 40
  • 56
Baraskar Sandeep
  • 698
  • 2
  • 8
  • 16
  • 1
    Yeah well...without any code or something to look at, we can't really tell you anything. Maybe [this question](http://stackoverflow.com/questions/4448467/cant-connect-to-local-mysql-server-through-socket-var-lib-mysql-mysql-sock) could help you. An on another note: Please do not use `mysql_*` functions. They are deprecated and have been replaced with [PDO](http://php.net/pdo) and/or [MySQLi](http://php.net/mysqli). – Till Helge Mar 19 '13 at 09:52
  • Sorry....I thought you wanted to reuse any of the Wordpress code, but apparently you only try to connect to the same database. As I mentioned above, have a look [here](http://stackoverflow.com/questions/4448467/cant-connect-to-local-mysql-server-through-socket-var-lib-mysql-mysql-sock) and please don't use `mysql_*` functions. – Till Helge Mar 19 '13 at 09:59
  • Thanks for reply @Till Helge Helwig But Please tell me what will i do now... if i want to connect to DB... am new to wordpress... – Baraskar Sandeep Mar 19 '13 at 09:59

2 Answers2

2

Try this

require_once(path/to/wp-config.php');
mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
Sibiraj PR
  • 1,481
  • 1
  • 10
  • 25
0

The key to using WordPress outside of WordPress is include the wp-load.php

// Include the wp-load'er
require_once("/path/to/wordpress/wp-load.php");

now you can use all the functions of wordpress in your php script

One problem with that solution is that it loads ALL the overhead of WordPress,

another way is as below

require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-config.php' );
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-includes/wp-db.php' );
$wpdb = new wpdb( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
Subodh Ghulaxe
  • 18,333
  • 14
  • 83
  • 102