retrieving multiple values with one mysql query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fishnfrogs
    New Member
    • Nov 2009
    • 20

    retrieving multiple values with one mysql query

    Hello everyone! I have an app that uses a for loop to go through an array and it selects all the values from my MySQL database and returns the values. I can't imagine that this is the best approach. My code kinda looks like this:

    Code:
    $array = array('blah', 'bleep', 'bloop');
    $password = 'same_value_for_each_item_in_the_array';
    $name = 'same_value_for_each_item_in_the_array';
    
    $userArray = array();
    $passArray = array();
    $nameArray = array();
    
    for($i=0; $i<count($array); $i++)
    	{
    	$result = mysql_query("SELECT * FROM `table` WHERE `user`='$array[$i]' AND `password`='$password ' AND `name`='$name ' ORDER BY `date` DESC LIMIT 60");
    		if($result) {
    			while ($row = mysql_fetch_object($result)) {
    			$userArray[] = $row->user;
    			$nameArray[] = $row->name;
    			$passArray[] = $row->password;
    			}
    		}
    	}
    Cna anyone offer up a better, more efficient way of going about this? Thanks in advanced!!
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    multiple requests can be more efficiently made using Prepared Statements.

    Comment

    • fishnfrogs
      New Member
      • Nov 2009
      • 20

      #3
      Hi, thanks for the reply. I actually ended up using a single query string and using a loop to concatenate to it. It all seems to be working quite well. It look my load time from about 15 seconds to about 2. YAY!

      Comment

      Working...