0

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

6
  • 1
    You have this code in your header.php file? If so, you should not be doing that Commented Apr 12, 2011 at 15:45
  • This kind of code should go in a plugin or at least your functions.php. Commented Apr 12, 2011 at 16:29
  • I see what you mean. But I need this code to work on OTHER parts of the site that are NOT wordpress driven. This code is in a sitewide inc-header.php include file. Included everywhere, not just wordpress. Commented Apr 12, 2011 at 16:37
  • Can you post your mysql connection code? That's where the problem lies, not in the sql statement. Commented Apr 12, 2011 at 16:58
  • @Ciaran - Sure. Thanks! Like I said, this works fine on NON-Wordpress pages. Thanks for the help! $x = mysql_connect($server,$dbuser,$dbpass,true) or die(mysql_error()); mysql_select_db($dbname,$x); Commented Apr 12, 2011 at 18:11

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

10 Comments

You can specify a database directly in the queries: SELECT fieldname FROM database.table .... Selecting a default database just removes the need from specifying the database name explicitly.
Exactly, this will plug in to OPs current setup without having to change all the queries...
Thanks for the info, but I have tried that. I now get "Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in <dirname>", ONLY on the wordpress pages.
You need to include the mysql_connect on the pages that use your own mysql_query...
@Dunhamzzz Well... that worked.... unfortunately. If I declare everything every single time, right before the SQL statement, it works. Which means I have to put the dbname, user, login, etc etc every single time I want to pull data from it. Grrrrr!! :) Surely there is another way? sniff sniff Thanks again!
|
1

@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!
?>

3 Comments

Hmmmm good idea! Let me throw this wrench in the machine though. I have code EVERYWHERE, on lots of different pages, that makes calls the traditional way to the database. I am really really trying to avoid changing all of those, especially since I am not that proficient with php and mysql connectivity yet.
@NarfFlarf: See my updated answer which might mean less modification to your existing code.
I agree with @stealthyninja to use the wpdb object. I believe you also have to add the following at the top of your file: <?php global $wpdb; ?>

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.