dtp791357 2015-11-05 09:34
浏览 74

从perl程序运行php脚本

My perl file and php file are in the same folder. I'm calling php script from perl in cmd

My database.pl script

#!/usr/bin/perl
while (2) 
{
   my $php = `sample.php`;
   sleep (10);

}

My sample.php file

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydb";   
$conn = new mysqli($servername, $username, $password, $dbname); 
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
$sql = "UPDATE users SET name1='Doe' WHERE id=1";
if ($conn->query($sql) === TRUE) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . $conn->error;
}
$conn->close();
?>

Now when i run database.pl file, my sample.php file is opening in an editor for every 10 second.. Now how should i make my php file to run, so that it should run UPDATE query.

  • 写回答

2条回答 默认 最新

  • dongpi9494 2015-11-05 09:37
    关注

    Depending on what operating system you are using, there are different ways. It seems like you are on Windows though.

    Probably your default action for the .php extension is to open it in the editor. Change the backticks line to explicitly run the script with the PHP interpreter.

    my $php = `php sample.php`;
    

    This will of course require that Windows knows where php is. It should be in your PATH variable.

    If you are on Linux, you can make the php script executable ...

    $ chmod u+x sample.php
    

    ... and call it from the current directoy.

    my $php = `./sample.php`;
    

    Note that you can just use DBI to do your SQL query from Perl, or implement that loop in PHP and reuse your DB connection there.

    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮