PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, November 12, 2022

[FIXED] How would I do this in php?

 November 12, 2022     loops, memcached, php     No comments   

Issue

I'm implementing a memcache to cache mysql query results.

It loops through the mysql results using

while($rowP3= mysql_fetch_array($result3)) { 

or it loops through the memcached results (if they are saved in memcache, and not expired) via

foreach($cch_active_users_final as $rowP3) {

How can I get it to show the right looping method based on if the memcached value exists or not. I just want it to pick the right looping method. I could just duplicate the entire while { } function with all its contents, but I don't want to repeat that huge chunk of code, just so I can change while to foreach


Solution

The logic should be something like:

(partial pseudocode)

$data = get_memcached_data('cch_active_users_final');

if (!$data) {
  $query = mysql_query(..);
  $data = array();
  while($row = mysql_fetch_array()) {
    $data[] = $row;
  }
  store_in_memcache('cch_active_users_final', $data);
}

// do whatever you want with $data


Answered By - Joel L
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing