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

Link to comment
Share on other sites

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");

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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