0

我想从一个文件一个接一个地运行三个 php 脚本。所有函数都已在每个文件中定义,因此当它们被调用(也称为运行)时,它们将执行操作。我认为下面的代码会运行所有 3 个,但它会在第一个文件完成运行后立即停止。有谁知道为什么?

谢谢

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

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

在脚本结束前使用最后一个函数运行进行编辑

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);
    }

        }

新代码

更换

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

//$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
    }

    }
4

1 回答 1

1

在代码的不同部分使用echo语句来确定失去控制的位置或程序退出的位置。

确保每当您从可能引发异常的命令中分配给变量时,在异常发生时捕获异常,或对其进行处理。或者至少在使用该对象之前验证非空性 (!is_null())。

于 2012-05-10T23:08:11.347 回答