简体   繁体   中英

Php query only performing one time

I have a PHP query which sends the messages to the people according to the time in MySQL database :if it is 8:00 pm it will send the message at 8:00 pm to those people who have that time in the database.The problem i'm having is that my php query is only sending the message to the first person in the database only it isn't looking at other users I have in my database. I am using now sms gateway in sending the message. Here is my 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;

        }
    }
}

Inside your while($row = $result->fetch_assoc()) the function was getting defined multiple time. Just move it out and if will works.

Proper way:

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

If you are really need to put inside, then avoid multiple declaration:

if (!function_exists('SendSMS')) {
    function SendSMS (...) {
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM