dongqing6661
2011-04-12 15:43
浏览 32
已采纳

Wordpress在wordpress页面上搞乱我的db调用?

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

图片转代码服务由CSDN问答提供 功能建议

嘿,这是怎么回事? 我有数据库冲突,我想要一些帮助。

基本上,我有一个标题可以引入随机数据库字段。 没什么特别的。 它位于我模板的标题中,在非WordPress页面上运行正常。

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

我使用相同的数据库,相同的用户名,相同的密码等。但是,我得到这个错误,其中echo应该是......

  警告:mysql_query()[function.mysql-query]:在&lt; dir name&gt; 
   
 
中拒绝用户'nobody'@'localhost'(使用密码:NO)  

显然,wordpress连接“覆盖”另一个(wordpress完美无缺)。 但是,我也很困惑,为什么它不起作用,虽然它使用相同的用户名和密码连接到同一个数据库。

任何人都可以帮我解决这个问题吗?

更新:发布连接代码

$ x = mysql_connect($ server,$ dbuser,$ dbpass,true)或死亡 (mysql_error()); mysql_select_db($ dbname,$ x);

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanchensou8685 2011-04-12 15:48
    已采纳

    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.

    打赏 评论
  • douhua1890 2011-04-12 15:58

    @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!
    ?>
    
    打赏 评论

相关推荐 更多相似问题