Jump to content

Can I search for a value from an array without extracting it?


SFADuncan

Recommended Posts

I have a function which enables me to obtain values from a database:

function fetch_user_lists($user_id, $list_title_id)  {
   $conn = db_connect();
 $query = "select * from user_lists where user_id='$user_id' and list_title_id='$list_title_id'";
   $result = @mysql_query($query);
   if (!$result)
     return false;
   $num_cats = @mysql_num_rows($result);
   if ($num_cats ==0)
      return false;  
   $result = db_result_to_array($result);
   return $result; 
}

Currently I'm able to obtain values I'm looking for via the following:

$user_lists = fetch_user_lists($user_id, $list_title_id);
  if (is_array($user_lists)) {
      foreach ($user_lists as $row) {
          $list_meta = $row['list_meta'];
          $content = $row['content'];

      if ($list_meta == 'list_title') { echo $content;  }
      }
   }

I was wondering... is there a function/command that might allow me to search for a value within an array without having go extract the array row by row, effectively saying "if you've found the result I'm looking for, do this" ?

 

Any help appreciated!

 

Simon

 

 

 

 

Link to comment
Share on other sites

Fix your SQL query so it only returns the information you want. I'm guessing

select content from user_lists where user_id='$user_id' and list_title_id='$list_title_id' and list_meta='list_title'
Oh, and don't SELECT * when you only need the one content column.
Link to comment
Share on other sites

Mmm... I feel a bit stupid because I didn't think of that.   :)

 

I guess that's because I may have, say, up to 300 rows of results.  But that makes sense actually.  

 

So instead of putting all the results in an array and attempting to search the array (or extracting the whole array and then searching), it's better to search for an individual result from the database directly, even if it has to do so up to 300 times?  Doesn't this put extra strain on the database (especially when it's happening via multiple searches at the same time?)

Edited by SFADuncan
Link to comment
Share on other sites

So instead of putting all the results in an array and attempting to search the array (or extracting the whole array and then searching), it's better to search for an individual result from the database directly, even if it has to do so up to 300 times?

Yep. But this "up to 300 times"... Are you talking about running this query hundreds of times in one script? I sure hope not.

 

Doesn't this put extra strain on the database (especially when it's happening via multiple searches at the same time?)

Not even close. It's what databases are specifically designed for.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.