Features to implement: monitor the user table and add it to the User2 table if a new record is added. (Practical applications can be a bit deeper, such as data processing, etc.)
Here is the PHP code (dbtest.php)
Copy Code code as follows:
!#/usr/local/php/bin/php
<?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 into the next cycle.
}
The business process should have nothing to say, there are a few places to look at:
The first line is the command path that the PHP CLI mode needs to add, and that while (1) and sleep (3), the rest are normal PHP code.
Through the shell command PHP dbtest.php Run, I tested on the virtual machine, the normal occupancy rate of CPU 0%, memory 1%.
Practical applications can be put in the background to run:
PHP dbtest.php &
BG 1
ps:& command Many places to say is not clear, even wrong. It just put the program into the background, and did not actually run!
Incidentally, review the Linux front and back run related commands
How to use the front and back state of a command
& background Pause added after command
BG background after running with the job number
FG Front desk after operation with the operation number
Ctrl+z background pause (key combination)
Jobs (view all job numbers) command