Jump to content

Recommended Posts

I'm trying to access a database using the contents of an array in the where clause as follows:

 

$query = "select image_id, image_name, category, prog_date from imagesumprog WHERE `prog_date` = $sumprogdate and category = '".$cat_titles[$i]."' ";

 

$cat_titles is the array which contains categories of image type. I know the array is populated and contains the values I need. If I echo the array, it displays the contents no problem. However, within this sql select statement it won't recognise the array. So if I echo the query I get this:

 

select image_id, image_name, category, prog_date from imagesumprog WHERE `prog_date` = 2010 and category = '' 

 

As you can see, there is no value for category at the end of the sql statement. It has not accessed the values in the array. Does anyone know why or how I can fix this? The strange thing is if I replace the variable $i in the statement with an actual index number then it works like this:

 

$query = "select image_id, image_name, category, prog_date from imagesumprog WHERE `prog_date` = $sumprogdate and category = '".$cat_titles[0]."' ";

 

Any ideas?

What was the value of $i?

 

Why not use:

$query = "select image_id, image_name, category, prog_date from imagesumprog WHERE `prog_date` = $sumprogdate and category IN ('".implode("',",$cat_titles)."') ";

 

Thanks Mark

The implode statement won't work as it takes all of the values in the array at once and tries to search the database as follows:

 

select image_id, image_name, category, prog_date from imagesumprog WHERE `prog_date` = 2009 and category IN ('Coláiste Samhraidh',Campa Samhraidh',Mini Campa')

 

This of course generates an error.

 

There are several category values and I must search the database with each value one by one.

select image_id, image_name, category, prog_date from imagesumprog WHERE `prog_date` = 2009 and category IN ('Coláiste Samhraidh',Campa Samhraidh',Mini Campa')

will generate an error because I missed a comma in the implode. It should be

implode("','",$cat_titles)

If you really have to loop through and issue several select statements (I can't understand why you need to):

foreach($cat_titles as $cat) {
   $query = "select image_id, image_name, category, prog_date from imagesumprog WHERE `prog_date` = $sumprogdate and category = '".$cat."' ";
   ...
}

 

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.