SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    problem combining php and ajax

    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
    PHP 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;
    }

    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.

    PHP Code:
    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. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so you used the exact same code, and it was working, but when you put it into a function it started giving you this problem?

    have you compared the output of the php script of when it was working to now? there must be a difference in the output.

  3. #3
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    code is exactly the same and worked perfectly. if i take out the echo code from the function, it works again. if i replace the echo with the return that i showed in the original post, it also works. the return doesnt give me the output that i need, though.

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The code to display the rows from mysql is totally functional but once put into the function below
    There is no code to display the rows in the function below.
    the page no longer reloads
    Since your goal is to avoid reloading the page, I would say you succeeded.
    nor shows the mysql query.
    And, how would you go about showing a mysql query?
    mysql_fetch_array($row)
    You might want to look up how to use mysql_fetch_array(). That ain't it.

  5. #5
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the mysql code isnt the problem. like i said before, in the part of my post you didn't quote--the mysql part is fine autonomously, it just doesn't work within the function

  6. #6
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the mysql code isnt the problem.
    Well, go read the docs on mysql_fetch_array() and then decide if the mysql stuff is fine.

    Also, post a complete working example of your problem. Trying to debug imaginary code is usually not very productive.

  7. #7
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the issue of mysql is immaterial, i can remove everything but an echo of the word "text" and it fails. removing the echo starts the reloading again. the mysql isnt the problem

  8. #8
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i still think you should compare the output of the working vs non-working version.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •