Hi,

I'm trying to access two databases and retrieve the same data from each but I only want one "No People are on" to be displayed if there is no one on in both databases.

If one user is online, in only one database, I would like only the users name listed and not the "No People are on" message from the database that has no one online.

This is the code I have but it's not working.

<?php
$handle_db1 = mysql_connect("localhost","db1","ascent") OR die(mysql_error());
$handle_db2 = mysql_connect("localhost","db2","ascent") OR die(mysql_error());
mysql_select_db("people",$handle_db1) OR die(mysql_error());
mysql_select_db("people",$handle_db2) OR die(mysql_error());
$query= mysql_query("SELECT name FROM people WHERE online ='yes' ORDER BY name"); $which = $handle_db1;
$query= mysql_query("SELECT name FROM people WHERE online ='yes' ORDER BY name"); $which = $handle_db2;
$count=mysql_num_rows($query); $which = $handle_db1;
if($count<1)
{
  $msg1="No People are on";
}
echo"$msg1";
while($row=mysql_fetch_array($query)){
$name = $row['name'];
echo "$name is On";
}
$count=mysql_num_rows($query); $which = $handle_db2;
if($count<1)
{
  $msg2="No Pople are on";
}
echo"$msg2";
while($row=mysql_fetch_array($query)){
$name = $row['name'];
echo "$name is "On;
}
?>

Any insight would be greatly appreciated.

Thank you.

You check the count for both queries and display the message thereafter. What you should do is store the count from both queries, add them, and when that result is zero then show your message.

Second, but less important, you could combine both queries into one (using UNION). Advantage is that the names from both DB's are sorted together.

SELECT name FROM db1.people WHERE online='yes'
UNION
SELECT name FROM db2.people WHERE online='yes'
ORDER BY name
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.