Php查询仅执行一次

时间:2017-05-12 18:17:19

标签: php html mysql

我有一个PHP查询,根据MySQL数据库中的时间将消息发送给人们:如果是晚上8点,它会在晚上8点将消息发送给那些在数据库中有时间的人我遇到的问题是我的php查询只是将消息发送给数据库中的第一个人,只是它没有查看我的数据库中的其他用户。我正在使用sms网关发送消息。 这是我的php:

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "hospital";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT *  FROM uap";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo " - Drug time: " . $row["drug1morning"]. "<br>";


        $hour = date('G');
        $min =date('i');
        $sec =date('sa');
        if ($hour == $row["drug1morning"]  && $min == 00 && $sec == 03 )  {

            function SendSMS ($host, $port, $username, $password, $phoneNoRecip, $msgText) {

                $fp = fsockopen($host, $port, $errno, $errstr);
                if (!$fp) {
                    echo "errno: $errno \n";
                    echo "errstr: $errstr\n";
                    return $result;
                }

                fwrite($fp, "GET /?Phone=" . rawurlencode($phoneNoRecip) . "&Text=" . rawurlencode($msgText) . " HTTP/1.0\n");
                if ($username != "") {
                    $auth = $username . ":" . $password;
                    $auth = base64_encode($auth);
                    fwrite($fp, "Authorization: Basic " . $auth . "\n");
                }
                fwrite($fp, "\n");

                $res = "";

                while(!feof($fp)) {
                    $res .= fread($fp,1);
                }
                fclose($fp);

                return $res;
            }

            $x   = SendSMS("127.0.0.1", 8800, "admin", "admin", $row["phonenumber"], "Hey there !!!!");
            echo $x;

        }
    }
}

1 个答案:

答案 0 :(得分:0)

在你的intersect内,该函数被多次定义。只需将其移出,如果可行的话。

正确的方法:

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

如果你真的需要放入,请避免多重声明:

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        //old code
    }
}
function SendSMS (...) {
}