Search code examples
phpmysqlmysqlimysqli-multi-querymulti-query

MySQLi Multi_Query Post


I am sending data to my PHP API using JSON. I want to send a push notification to my Android user with this API. I am facing one strange issue in it. From JSON, I am sending Mobile Number, Mobile Number Status and User Id.

I have two table in my database:

  1. user which has fields called id, fcm and fcm_enabled
  2. number_list which has fields called number, name and user_id

Now I want to send a push notification to whichever number status we get from the JSON in 1. To send the notification it needs to check if fcm_enabled=1, then it needs to get the fcm key from the fcm field from table 1's user, and name from table 2's name.

It's working fine under normal conditions, but if two users have the same mobile number, then I am getting two notifications on one device, and one notification on the other.

I think there's something wrong in my query. Let me know if someone can help me solve my issue. I have been trying from the last two days but, have had no success.

$number = $_GET["number"];
$status = $_GET["status"];
$userIds = $_GET["userId"];
$sql = "";
for($i = 0; $i < count($number); $i++) {
    if($status[$i] == 1) {
        $sqlSelect = "SELECT t2.name, t1.fcm, t1.fcm_enabled FROM user AS t1 INNER JOIN number_list AS t2 ON t1.id = t2.user_id WHERE t2.number = '$number[$i]'";
        $resultSelect = $conn->query($sqlSelect);
        if($resultSelect) {
            while($row = $resultSelect->fetch_row()) {
                if($row[2] == 1) {
                    sendFCM(array("title" => $row[0] . " is Online", "body" => ""), array("message" => ""), $row[1]);
                }
            }
        }
    }
}

Thanks.


Solution

  • If there is same number is for two users, may be it fetches duplicates records of users. "Group By" the number for fetching can help you.

    Updated the query with following:

    $sqlSelect = "SELECT t2.name, t1.fcm, t1.fcm_enabled FROM user AS t1 INNER JOIN number_list AS t2 ON t1.id = t2.user_id WHERE t2.number = '$number[$i]'  GROUP BY t1.id";