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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么