如何在我的“index.php”中连接到wordpress的DB,我需要将其放在“wp-content”文件夹中

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'

Try this

require_once(path/to/wp-config.php');
mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

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);