SFADuncan Posted August 16, 2013 Share Posted August 16, 2013 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 Quote Link to comment Share on other sites More sharing options...
requinix Posted August 16, 2013 Share Posted August 16, 2013 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. Quote Link to comment Share on other sites More sharing options...
SFADuncan Posted August 16, 2013 Author Share Posted August 16, 2013 (edited) 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 August 16, 2013 by SFADuncan Quote Link to comment Share on other sites More sharing options...
requinix Posted August 16, 2013 Share Posted August 16, 2013 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. Quote Link to comment Share on other sites More sharing options...
SFADuncan Posted August 16, 2013 Author Share Posted August 16, 2013 Well... can't thank you enough... even if you had to point out the obvious! Am very grateful. Thanks. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.