Home >Backend Development >PHP Tutorial >click.simba.taobao.com Simple real-time database monitoring and scheduling through PHP CLI
Function to be implemented: Monitor the user table. If a new record is added, add it to the user2 table. (Practical applications can be more in-depth, such as related processing of data, etc.)
The following is the PHP code (dbtest.php)
Copy the code The 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); //Enter the next loop after 3 seconds
}
Commands Front and backend Status Usage
& Background Pause Add after the command
bg Backend Run followed by job number
fg Front Desk Run followed by job number
Ctrl +Z Backstage Pause (Key combination)
jobs (View all job numbers) Command
The above introduces the simple real-time database monitoring and scheduling of click.simba.taobao.com through PHP CLI, including the content of click.simba.taobao.com. I hope it will be helpful to friends who are interested in PHP tutorials.