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.

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);
 var_dump(mysql_fetch_array($q));

As i say. If i execute the file alone:

http://example.com/blog/connect.php

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?

share|improve this question

2 Answers 2

up vote 4 down vote accepted

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)

share|improve this answer
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. –  m0r7if3r Feb 20 '12 at 17:55
    
i'm trying now. This one works just as expected! thanks! –  nax Feb 21 '12 at 8:53

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);
share|improve this answer
    
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 '12 at 17:42

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.