0

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);

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'];
}

and then i need to use

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

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.

thanks in advance.

2 Answers 2

2

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)

giving the benefits of:

  • cache the rs at php (serialized data)
  • jump backwards in rs
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the quick response, i reffer you to the answer above for my comment. Regards.
1

Yes there is a better way. You can first declare an array and save the fetched content there and then loop through the array. like this.

 $fetched_arr = array();
while ($full_alarm_fetch = mysql_fetch_array($full_alarm_query)) {
  $fetched_arr[] = $full_alarm_fetch;
}

then you loop the array

foreach($fetched_array as $something)
 {
    //here you can save the values in the $content
    // you can use $somethin['date'] and etc.. 
 }

3 Comments

thanks for the quick response, both of you . but than again adding another function (for loop) for pulling two variables that i already got in another one just doesn't sound right. isn't it too much code for information i already have on the script? another way i could do that is by stting the variable $client_name as FALSE , before the loop and in the loop make an if(!$client_name) {$client_name = $statics_fetch['number'] . " | " . $statics_fetch['name'];}, and than the setting of the variable will only occur once, but thanagain the if statement is now repeating it self..
@FedeSc: I wouldn't use another loop, too. This would definitly be more costly in execution time than overwriting the variable in each cycle. Like you said, you could either check if the variable already has a value, or, since you know the number before, you could make a seperate query just for the name. This would only be beneficial if you expect many rows in your main query, though. Either way, this is not a thing to lose too much time about, array lookup and variable assignment are very fast operations
@cypherabe - yeah i thought about another query also, considering the execution time. so i think i'll stick to the value varification method . thanks for all the consultation in this matter . Cheers Fede.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.