Features to implement: monitor the user table and add it to the User2 table if a new record is added. (Practical application can be a bit deeper, such as data related processing, etc.)
Here is the PHP code (dbtest.php)
Copy CodeThe code is as follows:
!#/usr/local/php/bin/php
mysql_connect (' localhost ', ' username ', ' password ');
mysql_select_db ("test");
Echo ' PID: '. Posix_getpid (). ' '; Current process PID (under Linux)
$old _id = 0;
while (1)
{
$sql = "Select ' id ' from ' user ' ORDER by ' id ' DESC LIMIT 1";
$result = mysql_query ($sql);
$item = Mysql_fetch_assoc ($result);
$new _id = $item [' id '];
$values _arr = Array ();
for ($i = $new _id; $i > $old _id && $old _id!=0; $i-)
{
$sql = "Select ' Name ', ' age ' from ' user ' WHERE ' id ' = ' {$i} ' LIMIT 1 ';
$result = mysql_query ($sql);
$item = Mysql_fetch_assoc ($result);
$name = $item [' name '];
$age = $item [' Age '];
$values _arr[] = "(' {$name} ', ' {$age} ')";
}
if (!emptyempty ($values _arr))
{
$values _str = Implode (', ', $values _arr);
$sql = "INSERT into ' user2 ' (' Name ', ' age ') VALUES {$values _str}";
mysql_query ($sql);
}
$old _id = max ($old _id, $new _id);
Sleep (3); 3 seconds to go to the next loop.
}
The business process should have nothing to say, there are several places to be aware of:
The first line is the command path that the PHP CLI mode needs to add, as well as the while (1) and sleep (3), and the rest are normal PHP code notation.
Through the shell command PHP dbtest.php Run, I test on the virtual machine, the normal occupancy rate of CPU 0%, memory 1%.
Real-world applications can be run in the background:
PHP dbtest.php &
BG 1
ps:& command Many places are very unclear, even wrong. It just puts the program in the background, and it doesn't actually run!
By the way, review the Linux front and back run related commands
How to use the command and back table status
& Background pause added after command
BG background run followed by job number
FG foreground operation followed by operation number
CTRL + Z background pause (key combination)
Jobs (view all job numbers) command
http://www.bkjia.com/PHPjc/320328.html www.bkjia.com true http://www.bkjia.com/PHPjc/320328.html techarticle features to implement: monitor the user table and add it to the User2 table if a new record is added. (Practical application can be a bit deeper, such as data related processing, etc.) the following is the PHP generation ...