Jump to content

problem with php and ajax


joerosenthal

Recommended Posts

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

[code]   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;
}
} [/code]

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.

[code]return join("\n", array_slice(mysql_fetch_array($row), -2)); [/code] 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.
Link to comment
Share on other sites

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 [color=green]FROM[/color] [color=orange]chat[/color] [color=green]ORDER BY[/color] `date`, `time` [color=green]ASC[/color] [color=green]LIMIT[/color] [color=orange]0, 4[/color]
[!--sql2--][/div][!--sql3--]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.