php多数据库问题
我已经设置了两个数据库连接,如下所示
$con1 = mysql_connect("localhost", "root", "pwd") or die (mysql_error());
$con2 = mysql_connect("localhost", "wordpress", "pwd", true) or die(mysql_error());
mysql_select_db("lab_ancp", $con1) or die(mysql_error());
mysql_select_db("wordpress",$con2) or die(mysql_error());
,它工作得很好,
所以然后我在这样的页面上做了一些查询:
$sql="select unome from associado where uid=$uid";
$result=mysql_query($sql,$con1) or die(mysql_error());
它工作得很好,之后我做了第二个这样的查询:
$sql="select ID, post_content, post_title, post_excerpt, meta_value
from wp_posts join (
select post_id, meta_value
from wp_postmeta
join (
select post_id from wp_postmeta
where meta_key='destaque' and meta_value='s'
)as t1 using(post_id)
where meta_key='pft_widescreen'
) as t2 on (wp_posts.ID=t2.post_id)
ORDER BY RAND() LIMIT 1";
//echo $sql . "<br />";
$row=mysql_fetch_assoc(mysql_query($sql,$con2)) or die(mysql_error());
一切都很好,但是然后。 ...
$sql="select * from eventos where edatade>='$hoje' or edataate>='$hoje'";
$result=mysql_query($sql, $con1) or die (mysql_error());
给出了这个错误:
**
SELECT 命令被拒绝给用户 表的“wordpress”@“localhost” '事件'
**
I've set two database conections as below
$con1 = mysql_connect("localhost", "root", "pwd") or die (mysql_error());
$con2 = mysql_connect("localhost", "wordpress", "pwd", true) or die(mysql_error());
mysql_select_db("lab_ancp", $con1) or die(mysql_error());
mysql_select_db("wordpress",$con2) or die(mysql_error());
and it works fine
so then i do some queries on a page like this:
$sql="select unome from associado where uid=$uid";
$result=mysql_query($sql,$con1) or die(mysql_error());
and it works fine, after that i do a second query like this:
$sql="select ID, post_content, post_title, post_excerpt, meta_value
from wp_posts join (
select post_id, meta_value
from wp_postmeta
join (
select post_id from wp_postmeta
where meta_key='destaque' and meta_value='s'
)as t1 using(post_id)
where meta_key='pft_widescreen'
) as t2 on (wp_posts.ID=t2.post_id)
ORDER BY RAND() LIMIT 1";
//echo $sql . "<br />";
$row=mysql_fetch_assoc(mysql_query($sql,$con2)) or die(mysql_error());
and again everything is just fine, but then....
$sql="select * from eventos where edatade>='$hoje' or edataate>='$hoje'";
$result=mysql_query($sql, $con1) or die (mysql_error());
gives this error:
**
SELECT command denied to user
'wordpress'@'localhost' for table
'eventos'
**
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从错误来看,您似乎应该验证 wordpress 用户对 eventos 表的权限。您的代码似乎是正确的。
如果您想验证这一点,可以尝试使用第二个连接“SELECT * from eventos”。将此作为脚本中的第一个查询执行。
From the error it seems you should verify permissions for the wordpress user on the eventos table. Your code seems to be correct.
If you want to verify this, maybe try a "SELECT * from eventos" using the second connection. Do this as the first query in the script.
好吧,
它解决了。
不要问我原因,但我尝试更改前两个 roww 中的顺序,即将 $con2 放在 $con1 之前,查询现在工作正常。
我怀疑...“true”参数与此有关。
谢谢大家。
Well
Its solved.
Don't askme the reason but i've tried to change the order in the first two roww, i.e put $con2 before $con1 and the queries now simply work fine.
I suspect that the ..."true" parameter has something to do with that.
Thx guys.
https://www.php.net/manual/ en/function.mysql-select-db.php#39095
https://www.php.net/manual/ en/function.mysql-select-db.php#93487
似乎是 mysql_select_db 的问题,第二个链接是一种解决方案。
我建议使用 phps mysqli (MySQL 改进扩展)而不是旧的mysql 的东西(不知道它是否解决了你的问题,但它解决了你可能遇到的其他问题)。
https://www.php.net/manual/en/function.mysql-select-db.php#39095
https://www.php.net/manual/en/function.mysql-select-db.php#93487
Seems to be a problem with the mysql_select_db, the second link is one solution.
I would recommend using phps mysqli (MySQL Improved Extension) instead of old mysql stuff (don't know if it solves your problem, but it solves other problems you might walk in to).
我怀疑原因是连接仍在内存中,因此当您尝试执行第二个查询时,将使用最后一个连接。我对 Joomla 两个数据库使用问题也有类似的问题。我正在寻找这个问题的解决方案,我认为这是我们的代码中缺少的规则。
嘿!!!我明白了,问题是当您创建第二个连接时,默认 PHP 返回与最后一个连接相同的引用。所以如果你需要一个新的连接,你应该准备一个 mysql_connect 并将 $new_link 设置为 true,就像这样
最后一个参数意味着你需要一个新的引用而不是最后一个。您可以找到更多信息:
此处和此处
希望有帮助。
I suspect that the reason is that the connection is still in memory so when you try to execute the second query, this use the last connection. I have the similar problem with Joomla two databases use issue. I am looking for the fix for this problem, I think that is a missing rule in our code.
HEY!!! I get it, the problem is that when you create the second connection, for default PHP return the same reference for the last one. So if you need a new connection you should prepare a mysql_connect with $new_link to true, like this
The last parameter means that you need a new reference and not the last one. You can find more information:
HERE and HERE
Hope it helps.