Joey Morani
Joey Morani

Reputation: 26611

How to keep checking a MySQL database?

Is it possible to keep checking a MySQL database and then update text? I'm using this to get the number of rows and then it's echo-ed:

$result = mysql_query("SELECT * FROM socialacc WHERE transid='$transID' AND access='$access'");
$i = 0;
while($row = mysql_fetch_array($result)) {
    $i = $i + 1;
}
echo $i;

I want this code to be repeated so that if there's ever another row that matches, the text would be updated without the page being reloaded. Would I use Javascript to repeat the check? Any idea's how I would go about it? Thanks for the help.

Upvotes: 0

Views: 166

Answers (2)

DaveRandom
DaveRandom

Reputation: 88697

You have two choices here: server push or AJAX.

Server push is probably simpler to program at the client side but quite complicated to write the server side in a way that won't eat some system resource or other on your server. AJAX more complicated at the client side, but simpler and easier to implement overall.

I would recommend the AJAX route for this job.

Upvotes: 2

deceze
deceze

Reputation: 522510

First of all, you can let the database count for you:

$result = mysql_query("SELECT COUNT(*) FROM socialacc WHERE transid='$transID' AND access='$access'");
$result = mysql_fetch_array($result);
echo $result[0];

Then, you'll probably want to repeat this query every so often through an AJAX call, but not so often that it'll kill your database.

Upvotes: 3

Related Questions