Jump to content


problem with php and ajax

  • Please log in to reply
1 reply to this topic

#1 joerosenthal

  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 29 March 2006 - 04:25 AM

I am using ajax to automatically reload the content from my mysql database on a given interval without refreshing the page. The code to display the rows from mysql is totally functional but once put into the function below

   function refresh() {
         mysql_connect(localhost,username,password) or die(mysql_error());
        mysql_select_db(main) or die(mysql_error());
$query = "SELECT postby,date,time,ip,msg FROM chat ORDER BY date,time ASC LIMIT 0,4;";
$result = mysql_query($query) or die('Error, query failed');
while($row = mysql_fetch_array($result))
list($postby, $date, $time, $ip, $msg) = $row;
$postby    = htmlspecialchars($postby);
$msg = htmlspecialchars($msg);
$msg = nl2br($msg);
echo $ip;

the page no longer reloads nor shows the mysql query. after troubleshooting, i noticed that echo and print seem to disrupt the javascript from working. upon removing the echo command the database is reloaded. even a simple echo of "text" brings the script to a failure.

return join("\n", array_slice(mysql_fetch_array($row), -2));
outputs correctly in the file but its not the output that I am looking for, so the problem isn't the javascript

any insight on this issue would be much appreciated.

#2 toplay

  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 29 March 2006 - 05:16 AM

You can't have PHP display anything. All that PHP should be doing is passing the data back and JavaScript has to display it.

I'm surprised that your query is even working as-is because it's not a good idea to use names of columns that are the same as MySQL reserved words (like date and time). I recommend changing the column names to something more meaningful that reflects what type of date and time is really for (i.e. post_date and post_time). However, if you chose to keep them the same, then enclose the column names within backtick marks. Example:

[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] postby, `date`, `time`, ip, msg FROM chat ORDER BY `date`, `time` ASC LIMIT 0, 4

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users