views:

78

answers:

4

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

It loops thru the mysql results using

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

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

foreach($cch_active_users_final as $rowP3) {

This is probably something thats really obvious... but Im not getting it.

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 dont wanna repeat that huge chunk of code, just so I can change while to foreach

Any tips?

A: 

You don't, you use a unified method using PDO.

e-satis
rewriting all mysql calls on the site isn't an option.
Yegor
Too bad, PDO is really the standard now, nobody uses mysql_query anymore. Actually, PDO is already quite rare since ORM do the most of the job.
e-satis
Nobody? :) I can agree with using PDO/ORM, but from there to say that **nobody** uses this or that anymore, specially considering the state of things with PHP is saying too much. Google "PHP tutorial" and you'll see that the things being taught to newcomers are way, way simpler than PDO/ORM. They don't even cover SQL Injection or XSS. :)
Carlos Lima
Learning and shipping are different things. Or course there is a lot of tuto around it, you must learn from the start (and by the way, they are tuto for that since 10 years, they are still there). When I say nobody use it anymore, I mean, nobody ship a software using mysql_query anymore. That wouldn't pass a code review in a big company, and small freelancers that uses that can't beat a concurrence that uses an ORM.
e-satis
But by the way, why would PDO be more complicated that mysql_query ? This is even simpler, you can use foreach !
e-satis
I'm not arguing for the mysql_query! :) I do agree with you. I'm just pointing out that "nobody uses it" was too strong a sentence. Specially because "a big company" that even does (fair) "code review" is far from the majority of users. Again, I do agree with you, just pointing out! :) When answering here we must keep in mind that sometimes changing this or that aspect of the design to what is considered "a better option" is out of the questioner's control or beyond deadline :).
Carlos Lima
Well, I can't disagree with your last point.
e-satis
A: 

If the $row and $rowP3 values contain the same data, you could have whatever happends in the loops in a function, and pass the row as an argument.

Marco
A: 

If I understand your question, this code doesn't make sense. After querying database comparing queried results to cached values doesn't make sense.

doc
It either loops thru the results on the while() **OR** it loop thru the the cached ones using the foreach. Not both.
Carlos Lima
+2  A: 

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
Joel L