Search code examples
phpmysqlphp-include

php: Execute php file from another php file with parameters


Details: I have a php file that runs some sql and retrieve a list of information. I run through a loop of that information and want to call another php page and pass it some parameters from the data I am looping through.

Question: How do I execute another php page from within my php?

What I Have Tried: This is the php code that should be calling the second php page (the while loop should be calling the simplepush.php page for each result I get):

<?php
require_once "../database/config.php";
header("Content-type: application/json");
$sql = "SELECT user_ip_address FROM ft_users";
$res = mssql_query($sql);
if (mssql_num_rows($res)) {

    while ($op = mssql_fetch_assoc($res)) {
        exec('simplepush.php?token = ' . $op . '');
        $arr[] = $op;
    }

    echo json_encode($arr);

    //$op = mssql_fetch_assoc($res);
    //$op['response'] = 200;
} else {
    http_response_code(420);
    $op = array(
        'response' => 420
    );
    echo json_encode($op);
}

mssql_close();
?>

I have tried the following:

include ('simplepush.php?token = '.$op.'');
exec ('simplepush.php?token = '.$op.'');
require ('simplepush.php?token = '.$op.'');
shell_exec ('simplepush.php?token = '.$op.'');

Solution

  • Just do:

    include ('simplepush.php');
    

    Now $op will be available in simplepush.php. Consider this example:

    //index.php
    while ($op = mssql_fetch_assoc($res)) {
        include ('simplepush.php');
        $arr[] = $op;
    }
    
    //simplepush.php
    print_r($op);
    

    The contents of $op will be output each time through the loop.