Jump to content

Select if id exists in array


flemingmike

Recommended Posts

hello, have a question.  im trying to do a select * where id exists in array $id8

 

here is my array

 

$result8 = mysql_query("SELECT * FROM customers WHERE whosclient = 'Lansoft'");
while($row8 = mysql_fetch_array($result8))
{
$id8=$row8['id'];
}

 

here is my select code.  it is only returning results for the highest number in the array

 

$result = mysql_query("SELECT * FROM timesheets WHERE status = 3 AND billable = 1 AND client = '$id8' ORDER BY date, starttime");
while($row = mysql_fetch_array($result))
{

 

Thanks for any help!

Link to comment
https://forums.phpfreaks.com/topic/229407-select-if-id-exists-in-array/
Share on other sites

im no expert but ill give it a shot:

$id8 = array();
$result8 = mysql_query("SELECT * FROM customers WHERE whosclient = 'Lansoft'");
while($row8 = mysql_fetch_array($result8))
{
$id8[].=$row8['id'];
}
$result = mysql_query("SELECT * FROM timesheets WHERE status = 3 AND billable = 1 AND client = '".in_array($id8)."' ORDER BY date, starttime");
while($row = mysql_fetch_array($result))
{

That what you were after?

Almost right:

 

$id8 = array();
$result8 = mysql_query("SELECT * FROM customers WHERE whosclient = 'Lansoft'");
while($row8 = mysql_fetch_array($result8))
{
$id8[] = $row8['id'];  // create array
}

 

$list = implode(',', $id8);  // implode array into comma seperated list to use in the IN clause
$result = mysql_query("SELECT * FROM timesheets WHERE status = 3 AND billable = 1 AND client IN ($list) ORDER BY date, starttime");

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.