Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-在循环中运行脚本_Php_Loops - Fatal编程技术网

PHP-在循环中运行脚本

PHP-在循环中运行脚本,php,loops,Php,Loops,我试图从一个脚本中运行一个脚本,但下面的脚本似乎不起作用。我基本上是从数据库中获取大量数据,循环遍历每一行并运行一个脚本,该脚本将根据这些数据生成XML set_time_limit(0); //connect to database $msSqlDB = new mySqlConnect('store'); $select = "SELECT * FROM FStores"; $run = mysql_query($select); while($row = mysql_fetch_a

我试图从一个脚本中运行一个脚本,但下面的脚本似乎不起作用。我基本上是从数据库中获取大量数据,循环遍历每一行并运行一个脚本,该脚本将根据这些数据生成XML

set_time_limit(0);

//connect to database
$msSqlDB = new mySqlConnect('store');

$select = "SELECT * FROM FStores"; 

$run = mysql_query($select);
while($row = mysql_fetch_array($run)){
    exec('/var/www/web/shop_xml/index.php?shopKeeper=$row[SKID]&shop=1');
}

最好的方法是什么?第二行是否会等待第一行成功执行,还是可以同时运行多个?非常感谢您的建议。

在exec命令中,您需要使用lynx(命令行浏览器)或PHP CLI运行脚本。您还应该引用域,而不是完整路径:

exec("php http://domain.com/index.php?shopKeeper=$row[SKID]&shop=1");

您的脚本不起作用,因为

exec('/var/www/web/shop_xml/index.php?shopKeeper=$row[SKID]&shop=1');
成功

exec("/var/www/web/shop_xml/index.php?shopKeeper=$row[SKID]&shop=1");
单引号中不能包含变量。

exec()尝试调用服务器文件系统上的程序,就像在shell提示下键入exec()参数一样。shell不知道如何处理URL,因此会出现“无匹配文件”错误,因为URL中的
将被视为shell通配符

如果你想请求一个url,你至少要这样做

$output = file_get_contents("/var/www/etc....");

PHP将为您执行完整的HTTP请求。

您是否尝试过使用此功能:

exec("/var/www/web/shop_xml/index.php?shopKeeper=$row[SKID]&shop=1");

而不是你原来的?单引号
不允许您计算其中的嵌入变量或php对象。您必须使用双引号
”--您能试试吗?

您需要这样做:

exec('php /var/www/web/shop_xml/index.php "'.escapeshellarg($row['SKID']).'" "1" > /dev/null 2>&1 &');
然后,在
index.php
脚本中:

<?php

  $shopKeeper = $argv[1];
  $shop = $argv[2];

  // ... do stuff
这将一次从数据库中获取
$perBatch
记录,并异步处理除最后一个记录外的所有记录。这将导致一次粗略处理
$perBatch
记录,并有助于避免大量进程占用服务器资源


作为旁注,您似乎在使用OO DB代码和过程DB代码的奇怪混合-您应该坚持使用其中一种代码以避免混淆。

George p的回答逐字回答了您的问题:如何从命令行按原样运行脚本。接下来,让我提供一些替代方案

首先,您的shop_xml/index.php脚本可以检查是否从命令行调用它,并相应地读取参数。因此:

if( php_sapi_name() == 'cli' ) {
   // you would, of course, want to escape these for malicious code!
   $shopKeeper = $argv[1];
   $shop = $argv[2];
}
else {
   $shopKeeper = $_GET['shopKeeper'];
   $shop = $_GET['shop'];
}
然后按如下方式调用命令:

$arg = escapeshellarg($row['SKID']);
exec( "/var/www/web/shop_xml/index.php \"$arg\" 1" );
include('functions.php');
set_time_limit(0);

//connect to database
$msSqlDB = new mySqlConnect('Freewebstore');

$select = "SELECT * FROM FacebookStores"; 

$run = mysql_query($select);
while($row = mysql_fetch_array($run)){
    processShopRow($row['SKID'], 1);
}
但是,更好的解决方案是从CLI调用index.php中的代码,并将其移动到include(如functions.php),将其放置在函数中,如下所示:

function processShopRow($showKeeper, $shop) {
   // stuff that used to be in index.php goes here
}
现在,在index.php和从CLI运行的代码中,您将包括并运行以下代码:

$arg = escapeshellarg($row['SKID']);
exec( "/var/www/web/shop_xml/index.php \"$arg\" 1" );
include('functions.php');
set_time_limit(0);

//connect to database
$msSqlDB = new mySqlConnect('Freewebstore');

$select = "SELECT * FROM FacebookStores"; 

$run = mysql_query($select);
while($row = mysql_fetch_array($run)){
    processShopRow($row['SKID'], 1);
}

您可能不会得到
这样的文件或目录:index.php?shopKeeper
引用问题,但是OP会留下这样一个事实:他试图在shell中执行URL,但会失败。您是对的,要在shell中执行脚本,您确实需要传递一个php或php5,还需要有php cli才能执行n脚本是正确的。不管怎样,我认为OP不知道他们想要什么。通过http调用本地脚本非常粗糙。感谢您的深入回答,分批执行此操作尤其有用。