NarfFlarf
NarfFlarf

Reputation: 63

Wordpress messing with my db calls on the wordpress pages?

Hey, hows it going everyone? I am having database conflicts I was wanting some help with.

Basically, I have a header that pulls in a random database field. Nothing special. It is in my header in my template and works just fine on non WordPress pages.

$sql = "SELECT * FROM headerslogans ORDER BY RAND() LIMIT 1";
    $result = mysql_query($sql);
    while ($myrow = mysql_fetch_array($result)) {
    echo $myrow['slogan'];
};

I am using the same database, same user name, same password, etc. However, I get this error where the echo should be....

Warning: mysql_query() [function.mysql-query]: Access denied for user 'nobody'@'localhost' (using password: NO) in <dir name>

So obviously the wordpress connection is "overwriting" the other (wordpress works flawlessly). BUT, I was also confused as to why it doesn't work, although it is connecting to the same database with same username and password.

Can anybody help me out on this?

UPDATE: Posted connection code

$x = mysql_connect($server,$dbuser,$dbpass,true) or die(mysql_error()); mysql_select_db($dbname,$x);

Upvotes: 0

Views: 252

Answers (2)

stealthyninja
stealthyninja

Reputation: 10371

@NarfFlarf: You could just use WordPress queries to get your data --

$sql = "SELECT * FROM headerslogans ORDER BY RAND() LIMIT 1";
$result = $wpdb->get_results($sql);
foreach ($myrow as $result) {
    echo $myrow->slogan;
}

See this WordPress Codex entry for further reference.



Update

Something else you can perhaps try then is to have <img src="slogan.php" alt=""> and within slogan.php:

<?php
/* Instantiate own, non-WP MySQL connection */
/* Run query and retrieve image */

header("Content-type: image/jpeg");
echo file_get_contents('path/to/image/' . $image); // 'dynamic' image!
?>

Upvotes: 1

Dunhamzzz
Dunhamzzz

Reputation: 14798

As you said, the connection is getting overwritten, is the database selection also getting over written? It's probable that actually mysql_select_db() is the thing screwing you over. Fix it by setting you your original connection as a variable, and referencing it in the offending mysql_* functions, eg:

$dbcnct = mysql_connect(...);
mysql_select_db('...', $dbcnct);
mysql_query('...', $dbcnct);

This shouldn't affect your current setup either.

Upvotes: 1

Related Questions