简体   繁体   English

PHP中的多个数据库连接

[英]Multiple database connectivity in php issues

My requirement is to query two databases. 我的要求是查询两个数据库。 Both the databases are in different hosts. 两个数据库都位于不同的主机中。 I have created two database connections con1 and con. 我创建了两个数据库连接con1和con。 If the query returns null for con1 then it would fetch data from con. 如果查询为con1返回null,则它将从con中获取数据。 But the code is not working as expected. 但是代码无法按预期工作。 Any help on the same would be greatly appreciated. 对此的任何帮助将不胜感激。 I am getting resource id #18 stored in get bookings even though the data is not present for the same in for database connected through con1. 我正在获取预订中存储的资源ID#18,即使通过con1连接的数据库中没有相同的数据。 Please help. 请帮忙。

This is the code that i have written in php. 这是我用php写的代码。

$get_bookings = mysql_query($SqlQry,$con1);

if (!$get_bookings) 
{
    $get_bookings = mysql_query($SqlQry,$con);
}

Data is mutually exclusive so both connections must be active . 数据是互斥的,因此两个连接都必须处于活动状态

Only connect to the second/backup database until you actually need it. 仅在实际需要时才连接到第二个/备份数据库。 Additionally if you want to check if you got results back from a query use mysql_num_rows() 另外,如果您想检查是否从查询中获得了结果,请使用mysql_num_rows()

"mysql_query returns false if there is an error not if there are no results" “如果有错误,则mysql_query返回false;如果没有结果,则mysql_query返回false”

if ( mysql_num_rows( $get_bookings ) > 0 ) //do something with results

You're not checking what you think your're checking. 您没有检查自己认为的内容。 Your're only checking if the query ran , not if it actually matched any rows. 您只检查查询是否已运行 ,而不是实际上是否与任何行匹配。 You need to do need something like mysql_num_rows to see if any data was returned. 您需要执行类似mysql_num_rows来查看是否返回了任何数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM