Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3235210
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:28:50+00:00 2026-05-17T17:28:50+00:00

I have code in php such as the following: while($r = mysql_fetch_array($q)) { //

  • 0

I have code in php such as the following:

while($r = mysql_fetch_array($q))
{

// Do some stuff

}

where $q is a query retrieving a set of group members. However, certain groups have there members saved in memcached and that memcached value is stored in an array as $mem_entry. To run through that, I’d normally do the following

foreach($mem_entry as $k => $r)
{

// Do some stuff

}

Here’s the problem. I don’t want to have two blocks of identical code (the //do some stuff section) nested in two different loops just because in one case I have to use mysql for the loop and the other memcached. Is there some way to toggle starting off the loop with the while or foreach? In other words, if $mem_entry has a non-blank value, the first line of the loop will be foreach($mem_entry as $k => $r), or if it’s empty, the first line of the loop will be while($r = mysql_fetch_array($q))

Edit

Well, pretty much a few seconds after I wrote this I ended up coming with the solution. Figure I’d leave this up for anyone else that might come upon this problem. I first set the value of $members to the memcached value. If that’s blank, I run the mysql query and use a while loop to transfer all the records to an array called $members. I then initiate the loop using foreach($members as as $k => $r). Basically, I’m using a foreach loop everytime, but the value of $members is set differently based on whether or not a value for it exists in memcached.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T17:28:51+00:00Added an answer on May 17, 2026 at 5:28 pm

    Not the ideal solution but i will give you my 2 cents. The ideal would have been to call a function but if you dont want to do that then, you can try something like this:

    if(!isset($mem_entry)){
     $mem_entry = array();
     while($r = mysql_fetch_array($q))
     {
    
       $mem_entry[] = $r;
    
     }
    }
    

    The idea is to just use the foreach loop to do the actual work, if there is nothing in memcache then fill your mem_entry array with stuff from mysql and then feed it to your foreach loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code with the following form: <?php function doSomething{ //Do stuff with MySQL
I have this PHP code echo '<a href=# onclick=updateByQuery(\'Layer3\', ' . json_encode($query) . ');>Link
I have some code on my PHP powered site that creates a random hash
I have the following php code, which has been snipped for brevity. I am
I have inherited some legacy PHP code what was written back when it was
I have some code I've written in PHP for consuming our simple webservice, which
i have some HTML code saved in a PHP string $str = <font size=2
I have the following code in header.php to echo the body's id: <body id=<?php
I'm mostly a Rails developer but sometimes, I have to code in PHP. Because
I have this php code, with which I am trying to generate a popup

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.