如何';追加';使用URL的函数变量和关于Array';s


How to 'append' function variables using the URL and a question about Array's

第一个问题是如何使用URL运行函数,我有以下函数:

function do_curl($start_index,$stop_index){
    // Do query here to get all pages with ids between start index and stop index
    $query = "SELECT * FROM xxx WHERE xxx >= $start_index and xxx <= $stop_index";

现在,当我尝试做curl.php时?start_ index=0&stop_index=2这不起作用,但当我删除函数并且WHERE idnum=1时,它起作用了。

现在第二个问题是如何将行中的所有字段"编译"为数组?我有当前代码:

    $query = "SELECT * FROM fanpages";
    $result = mysql_query($query) or die(mysql_error());
    while ($row = mysql_fetch_array($result))
{
    $fanpages_query = '''http://graph.facebook.com/'.$row['page_id'].''', ';
    echo $fanpages_query;    
}

$fanpages = array($fanpages_query);
$fanpages_count = count($fanpages);
echo $fanpages_count;

echo$fanpages_query;返回

'http://graph.facebook.com/AAAAAA', 'http://graph.facebook.com/BBBBBBB', 'http://graph.facebook.com/CCCCCCCC',

(我不知道如何用不同的方式来做,而且当我用这种方式做的时候,我不能删除最后一个逗号,这会返回PHP错误。)

echo$fanpages_count;返回1,正如你所看到的,我有3。

提前感谢各位!

执行函数调用以执行查询

function do_curl($start_index, $stop_index){
    ...
}
$fanpages = do_curl($_GET['start_index'], $_GET['stop_index']);

对于第二个问题,您可以使用数组和内爆函数插入逗号:

while ($row = mysql_fetch_array($result))
{
    $fanpages_query[] = 'http://graph.facebook.com/'.$row['page_id'];  
}
return $fanpages_query;

然后使用内爆打印出来:

echo implode(',', $fanpages);

整个代码:

function do_curl($start_index = 0, $stop_index = null) {
    $queryIfThereIsNoStartIndex = '';
    $queryIFThereIsNoStopIndex = '';
    $queryIfBothStartAndStopIndexAreMissing = '';
    $result = mysql_query($query) or die(mysql_error());
    while ($row = mysql_fetch_array($result))
    {
        $fanpages_query[] = 'http://graph.facebook.com/'.$row['page_id'];  
    }
    return $fanpages_query;
}
$fanpages = do_curl($_GET['start_index'], $_GET['stop_index']);
$fanpages_count = count($fanpages);
echo implode(',', $fanpages);

您应该完全使用mysql_escape_string来转义添加到查询中的值。