Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with Recalling Recent Messages

Status
Not open for further replies.

pollock77

Programmer
Aug 9, 2004
25
US
Okay, the following code is for a type of chat room in which, either messages that are less than 15 minutes old, or the most recent 25 messages will be printed. However, all that is being printed is the HTML tag and the Backround tag, which are not in the PHP coding. And, yes, I do have a message in the Database that should be printed, but is not. Can anyone offer any help?

Code:
<html>
<body background="tschat.png" properties="fixed">

<?php

mysql_connect("localhost", "chat", "password") or die(mysql_error());
mysql_select_db("chat") or die(mysql_error());

$time = date("U");
$display = $time-900;
$entries = mysql_query("SELECT COUNT FROM messages WHERE date >= $display");

$result = mysql_query("SELECT * FROM messages WHERE date >= $display") or die(mysql_error()); 
while($row = mysql_fetch_array( $result )) {
   if ($entries > '25'){
      for ($i='1'; $i<='25'; $i++){
         echo "<table width='600' border='1' cellspacing='0' cellpadding='1' bordercolor='white'>";
         echo "<tr>";
         echo "<td width='150'>";
         echo $row['date'];
         echo "</td>";
         echo "<td width='406'>";
         $result2 = mysql_query("SELECT * FROM users WHERE username='$row[username]'") or die(mysql_error()); 
            while($row2 = mysql_fetch_array( $result2 )){
               echo $row2['handle'];
            }
         echo "</td>";
         echo "<td rowspan='2' height='44' width='44'>";
         $result2 = mysql_query("SELECT * FROM users WHERE username='$row[username]'") or die(mysql_error()); 
            while($row2 = mysql_fetch_array( $result2 )) {
               echo "<img src='";
               echo $row2['av'];
               echo "\'>";
            }
         echo "</td>";
         echo "</tr>";
         echo "<tr>";
         echo "<td colspan='2' width='556'>";
         echo "Info";
         echo "</td>";
         echo "</tr>";
         echo "<tr>";
         echo "<td colspan='3' width='600'>";
         echo $row['message'];
         echo "<br>";
         echo "</td>";
         echo "</tr>";
         echo "</table><br><br> ";
         echo "</body>";
         echo "</html>";
      }
   }
   elseif ($entries <= '25'){
      for ($i='1'; $i<=$entries; $i++){
         echo "<table width='600' border='1' cellspacing='0' cellpadding='1' bordercolor='white'>";
         echo "<tr>";
         echo "<td width='150'>";
         echo $row['date'];
         echo "</td>";
         echo "<td width='406'>";
         $result2 = mysql_query("SELECT * FROM users WHERE username='$row[username]'") or die(mysql_error()); 
            while($row2 = mysql_fetch_array( $result2 )){
               echo $row2['handle'];
            }
         echo "</td>";
         echo "<td rowspan='2' height='44' width='44'>";
         $result2 = mysql_query("SELECT * FROM users WHERE username='$row[username]'") or die(mysql_error()); 
            while($row2 = mysql_fetch_array( $result2 )) {
               echo "<img src='";
               echo $row2['av'];
               echo "\'>";
            }
         echo "</td>";
         echo "</tr>";
         echo "<tr>";
         echo "<td colspan='2' width='556'>";
         echo "Info";
         echo "</td>";
         echo "</tr>";
         echo "<tr>";
         echo "<td colspan='3' width='600'>";
         echo $row['message'];
         echo "<br>";
         echo "</td>";
         echo "</tr>";
         echo "</table><br><br> ";
         echo "</body>";
         echo "</html>";
      }
   }
   else{
   }
}
?>
 
which is the limiting factor - the 25 entries or the time?

ie. if there are less than 25 entries in the previous 15 minutes do you still want to retrieve 25 entries?

conversely if there are more than 25 entries in the last 15 minutes do you want to retrieve them all or just the most recent 25?

 
by the way, the syntax for counting rows is select count(*) from tablename so your $entries line should be:

Code:
$res = mysql_query("SELECT COUNT (*) as cnt FROM messages WHERE date >= $display")
$row=mysql_fetch_assoc($res);
$entries = $row['cnt'];
 
I get an error now.

Code:
9  $time = date("U");
10 $display = $time-900;
11 $res = mysql_query("SELECT COUNT (*) as cnt FROM messages WHERE date >= $display");
12 $info=mysql_fetch_assoc($res);
13 $entries = $info['cnt'];

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/pollock7/public_html/new/blank.php on line 12
 
your sql statement is invalid!

what datatype is your date column? i also dimly recall that date is a keyword - you might want to enclose it, if you are to use it as a column name, in backticks (`)

as a matter of course i always put by sql variables in quotes.
Code:
$res = mysql_query("SELECT COUNT(*) as cnt FROM messages WHERE `date` >= '$display'");
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top