0

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.

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by pritaeas
0

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

Edited by pritaeas: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.