5 Replies - 587 Views - Last Post: 19 August 2011 - 01:10 PM Rate Topic: -----

#1 ahsanalishahid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 20-October 10

Database Query Question

Posted 19 August 2011 - 08:44 AM

function getfriendswallposts($wallpostids)
{
	foreach($wallpostids as $i => $v)
	{
		$query = "SELECT * FROM wall where wallpostid = '$v' ";
		$get = mysql_query($query) or die(mysql_error());
		while($row = mysql_fetch_assoc($get) )
		{
			$user = $row['user_id']; //
			
		}

}



}
$wallpostids is an array. Is there any way that without using a loop, using a single query, can I pass a set of values to get result from the database.

I hope u get my point what I wanna ask. :/ sorry for my bad english :/

This post has been edited by ahsanalishahid: 19 August 2011 - 08:45 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Database Query Question

#2 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 934
  • View blog
  • Posts: 2,332
  • Joined: 15-February 11

Re: Database Query Question

Posted 19 August 2011 - 09:06 AM

MySQL has a comparison operator called IN. It handles multiple values and returns the row if one of those values is located in the column being checked.
The following example checks whether 'hello' or 'world' is located in the column field and if so the row is returned.
SELECT * FROM `table` WHERE `column` IN('hello', 'world');


To convert your array into a valid PHP has a function called implode. This separates the elements of an array then joins them into a string based on the separator provided.
<?php
$array = array('hello', 'world');
echo (implode(',', $array));
?>


To get a valid SQL query we'll have to do something like this
<?php
$array = array('hello', 'world');
$query = "SELECT * FROM `table` WHERE `column` IN('" . implode("','", $array) . "')";
echo $query;
?>


Was This Post Helpful? 3
  • +
  • -

#3 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2486
  • View blog
  • Posts: 8,533
  • Joined: 08-August 08

Re: Database Query Question

Posted 19 August 2011 - 09:09 AM

Of course, if you're going to do that then you had better be sure the data is safe. Better yet, use PDO.
Was This Post Helpful? 3
  • +
  • -

#4 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 934
  • View blog
  • Posts: 2,332
  • Joined: 15-February 11

Re: Database Query Question

Posted 19 August 2011 - 09:15 AM

Using Prepared Statements with implode's going to be a bit tricky since the quotes that separate the values are going to be escaped making it one long string to MySQL.

The only thing I can think of is using array_walk then manually escaping the array elements yourself.
Was This Post Helpful? 1
  • +
  • -

#5 AdaHacker  Icon User is online

  • Resident Curmudgeon

Reputation: 433
  • Posts: 789
  • Joined: 17-June 08

Re: Database Query Question

Posted 19 August 2011 - 10:28 AM

*
POPULAR

View Postcodeprada, on 19 August 2011 - 12:15 PM, said:

Using Prepared Statements with implode's going to be a bit tricky since the quotes that separate the values are going to be escaped making it one long string to MySQL.

That's not necessary. You can just generate an array of unnamed place-holders and insert that into the string. It's especially easy in this case, where all the parameters get passed to the IN clause.
$array = array('hello', 'world');
$placeholders = implode(', ', array_fill(0, count($array), '?'));
$query = "SELECT * FROM `table` WHERE `column` IN ($placeholders)";
$statement = $pdo->prepare($query);
$statement->execute($array);


Was This Post Helpful? 5
  • +
  • -

#6 ahsanalishahid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 20-October 10

Re: Database Query Question

Posted 19 August 2011 - 01:10 PM

Thanks all :) I got the point :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1