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

 

 

 

 

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.

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.