external database query works in sidebar, not on posts
-
I wrote a plugin with some functions to query an external database with read-only access. One function executes a query and returns results specified in the args to my sidebar widgets. Works great:
function query($category,$imgsize,$numberofimgs) { $j = 0; while ( $j < $numberofimgs) { include 'config.php'; $db_server = mysql_connect($host, $user, $pass); if (!$db_server) die("Unable to connect to database: " . mysql_error()); mysql_select_db($database) or die("Unable to select database: " . mysql_error()); $query = "SELECT * FROM {$category} ORDER BY RAND()"; $result = mysql_query($query,$db_server); $row_cnt = mysql_num_rows($result); $row = mysql_fetch_row($result); echo "{$row[0]} of {$category} size={$imgsize}"; // etc… $j++; } mysql_close($db_server); }This returns a specific output to the sidebar for the browser
Now, I wrote another function that only executes the mysql_select_db() portion of the above, with no args, simply to connect to the database so I can do whatever I needed with the data in posts. This function fails to connect, but is exactly the same method as the above function:
function get_stuff() { include 'config.php'; global $db_server; $db_server = mysql_connect($host, $user, $pass); if (!$db_server) die("Unable to connect to database: " . mysql_error()); mysql_select_db($database) or die("Unable to select database: " . mysql_error()); }When I call get_stuff() in a post and preview it, it comes back with “Unable to connect to database: “. I even published the post, but again, “Unable to connect to database: “. How is it that one function can connect successfully and execute for sidebar widgets and unsuccessfully for posts?
The topic ‘external database query works in sidebar, not on posts’ is closed to new replies.