Click to See Complete Forum and Search --> : Trouble with an array in a SELECT query


gianna
05-20-2004, 01:42 PM
I'm pretty new at this.

So I have this array called $nextthreemonths which contains three values, say.. 05, 06, and 07.

I have this SELECT query that reads:$query = "SELECT * FROM $table WHERE (month = $nextthreemonths AND year = $today_year) OR (month = $today_month AND year = '0') ORDER BY day";...which produces a "mysql_numrows(): supplied argument is not a valid MySQL result resource" warning.

What is the proper way of handling an array inside a SELECT query?

Thanks much,
-g

Khalid Ali
05-20-2004, 02:23 PM
array is an object that acts as storage for multiple values(usually of same type in hard core programming languages).

in your case it seems like you wnat to pass a single value to month='6'
if thats the case then you have to point to the index in the array who's value yo want to insert into the query.
Now suppose your array had only 3 values

$nextthreemonths = array("05","06","07");

and you wanted 06 to be the value to be inserted into the query,then keeping in mind that array index starts from 0 being the first item,your query may look like this

$nMonth = $nextthreemonths[1];//get second value
$query = "SELECT * FROM $table WHERE (month = $nMonth

hope this helps

gianna
05-20-2004, 02:31 PM
Khalid, thanks for the quick reply.

Maybe I didn't make what I wanted clear enough. This is a simple calendar script that stores events by date. I want to pull out all the records where month = one of the months in my $nextthreemonths array.

Make sense?

Thanks,
-g

solavar
05-20-2004, 02:59 PM
First of all, you need to quote your variables


$sql = "SELECT * from mytable WHERE id = '$id'";


Secondly, if the table has a field called month, where you've got months entered as 01, 02, 03, 04,...,11, 12

you can not say

$nextthreemonths = array("05","06","07");
and then say...
$sql = "SELECT * FROM mytable WHERE month = '$nextthreemonths';


...instead, you can say the following...

$sql = "SELECT * FROM mytable WHERE month IN (01, 02, 03, 04)";



Thirdly...

Well I have started using backticks, these little things that look like this... ```````

For example, a typical sql statements reads...

$sql = "SELECT * FROM mytable WHERE `month` IN (01, 02, 03, 04)";

The reason for using backticks is that if the field name turns out to be a MySQL reserved word, then there won't be an error. Incidentally, month is not a reserved word, but
year_month is.

For a full list of MySQL reserved words, see

http://dev.mysql.com/doc/mysql/en/Reserved_words.html

Khalid Ali
05-20-2004, 03:27 PM
yep..as solavar mentioned in his post.
if you need to get values that should match either or then you need to use IN as compare to = operator.