user3680441
user3680441

Reputation: 11

Receiving data from database, constantly... how?

Im creating a chat box using php. Everything goes perfect, uploading and receiving.

The problem is that the code isn't searching or getting new data. My question is: how do I constantly get the results from my database?

My code to fetch the results is :

while($row = mysql_fetch_array($data)) 
 { 
      //echo the list item
      echo '
      <div class="message">
        <li>' . $row['id_from'] . ' <span>' . substr($row['time_send'], 11, 5) . '</span></li>
        <li>' . $row['message'] . '</li>
      </div>
      ';  
 }

Upvotes: 1

Views: 73

Answers (1)

i-CONICA
i-CONICA

Reputation: 2379

You need a JavaScript (probably using a library like jQuery too) to make AJAX requests to your server, to that script, which can then check the database for new records and output them, returning them to your waiting JS, which will then update the DOM with the new data.

To elaborate on your issue, by the way you describe it, you're expecting it to run as a process, watching, but it's not. It's a script. It's going to run once, in a split second, then finish. So the above will call the script over and over again once per reasonable time period (2 seconds?) and display the updated data.

Look at http://api.jquery.com/jquery.ajax/

Upvotes: 1

Related Questions