0

I want to run three php scripts one after another from one file. all the functions are already defined in each of the files so that when they are called upon (aka run) they will do things. I thought the following code would run all 3 but it stops right after the first file is finished running. does anyone know why?

Thanks

<?
error_reporting(E_ALL);ini_set('display_errors', 1);

require_once ("file1.php");
require_once ("file2.php");
require_once ("file3.php");
?>

EDIT WITH LAST FUNCTION RUN BEFORE END OF SCRIPT

function sendPush ($date)
 {

$username='xxxx';
$password='xxxxx';
$database='xxxxx';

$device="";

$db= new mysqli('localhost', $username, $password, $database);

if (mysqli_connect_errno())
{
    echo 'Error: Could not connect';
    //exit;

}


$query = "SELECT * FROM `DataTokens`";


$result = $db->query($query);
if ($result) {
    $num_results = $result->num_rows;

while($row = $result->fetch_assoc())
{

        if (!$row)
        {
            echo "No Token, insert into database";


        }


    $device= $row['Number'];


        // Put your device token here (without spaces):
        $deviceToken = $device;

        // Put your private key's passphrase here:
        $passphrase = 'XXXXXX';

        // Put your alert message here:
        $message = 'This is a test and its working';

        $i=1;
        $number= $i++;


        $ctx = stream_context_create();
        stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
        stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

        // Open a connection to the APNS server
        $fp = stream_socket_client(
            'ssl://gateway.sandbox.push.apple.com:2195', $err,
            $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

        if (!$fp)
            exit("Failed to connect: $err $errstr" . PHP_EOL);

        echo 'Connected to APNS' . PHP_EOL;

        // Create the payload body
        $body['aps'] = array(
            'alert' => $message,
            'sound' => 'default',
            'badge' => +1,
            'loc-key' => 'australia1'
            );


        // Encode the payload as JSON
        $payload = json_encode($body);

        // Build the binary notification
        $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;

        // Send it to the server
        $result = fwrite($fp, $msg, strlen($msg));

        if (!$result)
            echo 'Message not delivered' . PHP_EOL;
        else
            echo 'Message successfully delivered' . PHP_EOL;

        // Close the connection to the server
        fclose($fp);
    }

        }

New Code

replaced

  $result = $db->query($query);
if ($result) {
    $num_results = $result->num_rows;

with

//$result = $db->query($query);
if ($result = $db->query($query)) {

echo "we have a problem";

}

else
{

 $num_results = $result->num_rows;

while($row = $result->fetch_assoc())
{

         //blah blah
    }

    }
21
  • 8
    is there die(); or exit(); in file1.php? Commented May 10, 2012 at 22:03
  • There's no reason why the code you've posted would not run all the files unless there is something stopping the execution of the script on file1.php Commented May 10, 2012 at 22:06
  • @ConradWarhol thanks for the quick reply. just a fclose($fp) that closes the connection to mysql Commented May 10, 2012 at 22:08
  • do you require file2 in file1? Commented May 10, 2012 at 22:10
  • 1
    I would advise writing all the functions in one file (called functions.php) and then write another file that is the logic controlling when each function is called, with what parameters, and what is done with the value (called driver.php) Commented May 10, 2012 at 22:37

1 Answer 1

1

Use echo statements at different parts of your code to figure out where control is being lost or where the program is exiting.

Make sure whenever you have an assignment to a variable from a command that could throw an exception, to catch the exception if it occurs, or do something with it. Or at least verify for non-null ness (!is_null()) before using that object.

Sign up to request clarification or add additional context in comments.

1 Comment

Topics to read up on: Exception handling, classes (and therefore objects), design patterns, best practices (dry, solid, kiss)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.