0

Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select

<?php 
$con=mysql_connect('localhost','root','');
mysql_selectDb('cms',$con);
$con2=mysql_connect('localhost','root','');
mysql_selectDb('cms2',$con2);
$memberId="Member0001";
$select2=mysql_query("select * from member_register where memberID='$memberId'",$con);
$row2=mysql_fetch_array($select2);
?>

i got a code like above, select 2 database from 1 server, sure the code is right, but it appear

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in xxx on line xxx

4

2 回答 2

2

There is no need to maintain two connections to the same DB server with the same credentials. You can trivally use

SELECT db1.table.member_register, ...

SELECT db2.othertable.somefield, ...

using the same single connection. Two connections would only really be needed if you have to connect using DIFFERENT credentials.

In your case, since you're getting a 'boolean received', your query failed, and you simply assumed it succeeded (NOT a good idea). Try:

$select2=mysql_query("...",$con) or die(mysql_error());
                                ^^^^^^^^^^^^^^^^^^^^^^

to find out WHY the query failed.

于 2012-12-14T16:02:13.830 回答
1

If you're passing the exact same connection details to mysql_connect, you'll get the same connection back. It's reusing existing connections. So you only have one connection, and your query fails because you're operating on the wrong database.

Use ... FROM database.table ... in your queries to work across different databases on the same server.

于 2012-12-14T16:04:00.003 回答