循环中获取外部变量

循环中获取外部变量

本文介绍了PHP 从 while 循环中获取外部变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 PHP 脚本中,我创建了一个 mysql_query :

in my PHP script i make a mysql_query :

  $full_alarm_sql = "
          SELECT alarms.id, alarms.date, clients.name, clients.number, alarms.controller, alarmstype.type,
          alarms.alarmstatus, alarms.starttime, alarms.endtime, DATEDIFF(CURDATE(), alarms.date) AS difference
          FROM alarms
          LEFT JOIN clients ON alarms.clientid = clients.id
          LEFT JOIN alarmstype ON alarms.typeid = alarmstype.id
          WHERE DATEDIFF(CURDATE(), alarms.date) <=" . $type . " AND clients.number = " . $market;
  $full_alarm_query = mysql_query($full_alarm_sql);

然后我在表中创建一个while循环来获取查询:

and then i make a while loop inside a table with the fetching of the query :

while ($full_alarm_fetch = mysql_fetch_array($full_alarm_query)) {
  $content .=
    "
           <tr>
          <td>" . $full_alarm_fetch['date'] . "</td>
          <td>" . $full_alarm_fetch['number'] . " | " . $full_alarm_fetch['name'] . "</td>
          <td>" . $full_alarm_fetch['controller'] . "</td>
          <td>" . $full_alarm_fetch['type'] . "</td>
          <td>" . $full_alarm_fetch['alarmstatus'] . "</td>
          <td>" . $full_alarm_fetch['starttime'] . "</td>
          <td>" . $full_alarm_fetch['endtime'] . "</td>
        </tr>";
   $client_name = $full_alarm_fetch['name'] . ' | ' . $full_alarm_fetch['number'];
}

然后我需要使用

$full_alarm_fetch['name'] . ' | ' . $full_alarm_fetch['number'];

所以我现在这样做的方式似乎并不是最好的方式,这意味着 while 循环中的变量会一次又一次地被覆盖,直到循环终止,然后我才能使用获取信息.

so the way i'm doing it now just doesn't seem the best way, meaning the variable inside the while loop is overwritten time and time again until the loop dies and than i can user the fetch information.

我想知道是否有更好/更有效的方法来做我想做的事情.

i was wondering if there is a better/efficient way of doing what i'm trying to do.

提前致谢.

推荐答案

你的代码很好.你可以一次抓取整个 rs,然后循环它,

Your code is fine. You could grab the whole rs at once, and then loop it,

while ($rs[] = mysql_fetch..);
//done..
for($i=0;$i<sizeof($rs);$i++) or
foreach($rs as $rec)

给予以下好处:

  • 在 php 中缓存 rs(序列化数据)
  • 在 rs 中向后跳转

这篇关于PHP 从 while 循环中获取外部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 10:13