doukai1226 2017-03-27 13:26
浏览 47
已采纳

PHP While Loop从数据库中获取数据

I'm building a mobile application that requires push notifications to be sent whenever a new post is added.

Here's the PHP notification script: (it should be added inside the loop so it will be used again for each result.

$to = (should get DEVICEID from the database).
$title = "A new request"
sendPush($to,$title,$message);
function sendPush($to,$title,$message){
  //Sends noti.
}

So How do I place the above code in a while loop, and get the DEVICEID from the database and place it into $to ?

  • 写回答

1条回答 默认 最新

  • doupeng6890 2017-03-27 13:36
    关注

    I suppose that you have an mysql connection set up and that you are using mysqli.

    After you have created the connection to the database, you should have a variable holding your connection:

    $con = mysqli_connect("localhost","my_user","my_password","my_db");
    
    // Check connection
    if (mysqli_connect_errno())
    {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    

    So right now you are able to execute a SQL statement to your database. Your SQL statement should look similarily to this:

    $sql = "SELECT DeviceID FROM Devices";
    

    Now we can execute the statement we created to the database:

    $result = mysqli_execute($con, $sql);
    

    Now we get a result object. We can use this to get a while loop:

    while ($row = mysqli_fetch_assoc($result)) {
        $to = $row["DeviceID"];
        // Rest of your code, this will be executed for every DeviceID in the database.
    }
    

    If you are using PDO you can read more about it here: http://php.net/manual/en/ref.pdo-mysql.php

    But in general, the steps are the same.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line