hi,thank you for your attention.:P

I am thinking of if there are some thing in php like event or trigger to inform another php file, :idea: that I'v just insert something into the mysql database .

thank you for giving me some advices~ :)

Recommended Answers

All 15 Replies

If you mean from mysql to php, then no, this is not possible. You'd have to create a polling mechanism.

i think really the only way you can try would be

$query = "SELECT * FROM `events` where `it` = 'awsome'"; 

 $result = mysql_query($q) or die(mysql_error());

if($result > 0){
// in here do something either include the file 
// or send it a http request

}

what are you trying to do maybe if you have a better describtion we can steer you th right way

Member Avatar for diafol

depends on how the DB is being updated. I assume you'd use php to update the mysql DB, so just use HITMAN's code. You can use include(...somefile...); to include code from another file. Is that what you need?

sorry, I should make a more clear scenario description.

say, userA post a message to post.php, which will insert message into the database.
meanwhile, userB post a get request, in ajax way, to get.php,
what I want to do is when post.php received the post request, and insert something to database, then it will inform get.php to start to get message from the database.
maybe in get.php, there should be a loop, checking if post.php put message in the database;
i just want to know, is there some event trigger model, or something else can help me to do that,


again, thank you so much for your attention and replies

i think really the only way you can try would be

$query = "SELECT * FROM `events` where `it` = 'awsome'"; 

 $result = mysql_query($q) or die(mysql_error());

if($result > 0){
// in here do something either include the file 
// or send it a http request

}

what are you trying to do maybe if you have a better describtion we can steer you th right way

is this what your mean? i create a table named events, and every time i insert something to database using php,i also insert a event value to the events table, and another php file keep checking this events table.

Member Avatar for diafol

Sounds like a repeating poll request, like a chat app, updating every so often. You can ajax the call to the server to find new data say every 30 seconds. That's easy.

However, some would argue the case for using comet, but for a small scale thing, a js setTimeout() calling an ajax script should be fine.

Sounds like a repeating poll request, like a chat app, updating every so often. You can ajax the call to the server to find new data say every 30 seconds. That's easy.

However, some would argue the case for using comet, but for a small scale thing, a js setTimeout() calling an ajax script should be fine.

yeah, it is a chat app, and i use the comet long polling model, when i load the user page, it will automatically send a ajax request to message.php, waiting for the event happen(the code blow), but i found a problem, i can't post (in post.php) any data to database. is that means that if one php file keep querying the database,mysql will block other query request in this long polling model.
and if i use the way you said before, keep sending ajax request in some fixed interval,say 30s, getting message and posting message will not interfere at all.

please help me, thank you :)

in message.php: it's used for someone to keep checking in server for new messages

$num=0;
while($num==0){

	$sql="select * from chat_message where event=1";
	$re=$db->fetchMessages($sql);
	$num=$db->getNum();
	if($num==0){
		usleep(1500000);
                clearstatcache();
	}
echo messages;
update sql and set event=0;

}

and in post.php

$something=$_POST['message'];
$sql="insert into chat_message (something,event) value($something,1)";
$db->query($sql);

Sounds like a repeating poll request, like a chat app, updating every so often. You can ajax the call to the server to find new data say every 30 seconds. That's easy.

However, some would argue the case for using comet, but for a small scale thing, a js setTimeout() calling an ajax script should be fine.

help me~~ I've been stuck here for days.

more details show below. the ajax request script.

<script type="text/javascript" charset="utf-8">
			
			function waitForMsg(){
				
				$.ajax({
					type: "GET",
					url: "message.php?hid=1",
					async: true,
					cache: false,
					success: function(data){
						//var json = jQuery.parseJSON(data);
						alert(data);
						setTimeout('waitForMsg()',1000);
					},
					error: function(XMLHttpRequest, textStatus, errorThrown){
						alert("error:"+textStatus+":"+errorThrown);
						setTimeout('waitForMsg()',15000);
					}
				});
			}
			$(document).ready(function(){
			waitForMsg();
			});
			</script>

So, what are you stuck with exactly ?

Member Avatar for diafol

OK, I can't see anything obvious wrong with that. However, this is js now.

Maybe you can use memcached. When Post does an operation you save a value on memcached, Get will do a loop in search of that value (stored in RAM), this will not affect the database until the condition is true. Example:

<?php
# Post
$query = mysql_query("INSERT INTO your_table () values()");

$m = new Memcached('mymemc');
$m->setOption(Memcached::OPT_PREFIX_KEY, 'myapp:');
$m->setOption(Memcached::OPT_DISTRIBUTION, Memcached::DISTRIBUTION_CONSISTENT);
$m->addServer('127.0.0.1',11211);

$m->add('cache','hello world!',100); # 100 seconds before disappearing
?>

On the other side:

<?php
# Get
$m = new Memcached('mymemc');
$m->setOption(Memcached::OPT_PREFIX_KEY, 'myapp:');
$m->setOption(Memcached::OPT_DISTRIBUTION, Memcached::DISTRIBUTION_CONSISTENT);
$m->addServer('127.0.0.1',11211);

if($m->get('cache') == true)
{
    $query = mysql_query("SELECT * FROM your_table ...");
    $m->delete('cache');
}
?>

More information: http://www.php.net/manual/en/book.memcached.php

OK, I can't see anything obvious wrong with that. However, this is js now.

thank you , anyway, forget it. :icon_smile:

So, what are you stuck with exactly ?

thank you for your attention, I was writing a web-based chat app, in the beginning, I try to find out a better way to solve a scenario,userA is chatting with userB, userA send a message to server, which store the message to mysql. meanwhile, userB post an ajax request to server, keeping finding out if there is new message from userA.
I use two way to solve it,
the first one, userB keeping sending the ajax request every 30s, to querying the database whether new message is stored.and it works, however, keep sending ajax requesting and querying is not a better way, some sort of wasting. so I use another way, that is, comet long polling model. so I rewrite the message.php and the ajax request, see the code above( message.php and the js script), but it did work, I can't post anything after script is running (the script start to run when the page loaded). I don't know what happened.

so what i was stuck with is two thing:
the first one, how to make the comet long polling model run correctly.
the second one, which is also the title, find some other way to solve this problem except keep sending polling request.

thank you ~

Maybe you can use memcached. When Post does an operation you save a value on memcached, Get will do a loop in search of that value (stored in RAM), this will not affect the database until the condition is true. Example:

<?php
# Post
$query = mysql_query("INSERT INTO your_table () values()");

$m = new Memcached('mymemc');
$m->setOption(Memcached::OPT_PREFIX_KEY, 'myapp:');
$m->setOption(Memcached::OPT_DISTRIBUTION, Memcached::DISTRIBUTION_CONSISTENT);
$m->addServer('127.0.0.1',11211);

$m->add('cache','hello world!',100); # 100 seconds before disappearing
?>

On the other side:

<?php
# Get
$m = new Memcached('mymemc');
$m->setOption(Memcached::OPT_PREFIX_KEY, 'myapp:');
$m->setOption(Memcached::OPT_DISTRIBUTION, Memcached::DISTRIBUTION_CONSISTENT);
$m->addServer('127.0.0.1',11211);

if($m->get('cache') == true)
{
    $query = mysql_query("SELECT * FROM your_table ...");
    $m->delete('cache');
}
?>

More information: http://www.php.net/manual/en/book.memcached.php

thank you so much, I'll try this way.

thank you so much for all of your replies. and sorry for responding so late.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.