I'm making a layout for a wordpress page.

The layout needs to connect to a second database (both databases are in the same domain).

But the problem is, when i execute just the file it works. But when i included inside the wordpress layout i don't get data, no errorrs, nothing

The script is just:

 $con = mysql_connect('host','user','pass');
 mysql_select_db('db', $con);
 $q = mysql_query('SELECT * FROM vars', $con);

As i say. If i execute the file alone:


it works great, and returns data.

But if i do inside the layout (creating a wordpress page, and giving the layout to it) Returns nothing.

Any idea what can be happening there?

2 Answers 2


I don't know why the mysql_connect isn't working inside WordPress. But an easier way of doing it would be to use the $wpdb class. Try replacing your code with this:

$second_db = new $wpdb( 'user', 'pass', 'dbname', 'host' );
$q = $second_db->get_results( 'SELECT * FROM vars' );
var_dump( $q );

and see if you get the results you want. (note that the order of the variables passed to the $wpdb constructor is different from the order used by mysql_connect)

  • 1
    And, just for the record, you should be using PHP PDO, not mysql_...which was designed LONG ago for MySQL4, which isn't even supported by wordpress anymore.
    – mor7ifer
    Feb 20, 2012 at 17:55
  • i'm trying now. This one works just as expected! thanks!
    – nax
    Feb 21, 2012 at 8:53
  • Set WPDB prefix to make WP_Query and get_post to generate correct sql query by calling $wpdb->set_prefix('wp_');
    – M-R
    Oct 13, 2015 at 10:57

goldenapples probably has the best solution, but to answer your question specifically, you need to set the new_link parameter in the mysql_connect function to true.

 $con = mysql_connect('host','user','pass', true);
  • I understand if you create 2 mysql_connect with the 3 params (no matters if them are differents) it return the same link? without that true. I just gonna test tomorrow at work, if don't work i test the goldenapples solution too. thanks
    – nax
    Feb 20, 2012 at 17:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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