Re: Queries and Arrays
| From: | Teodor Cimpoesu | Date: | Wed, 27 Dec 2000 12:16:21 +0000 |
| Subject: | Re: Queries and Arrays | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-31946@lists.php.net to get a copy of this message | ||
Hi RealGM!
On Wed, 27 Dec 2000, RealGM Webmaster wrote:
>
> Hi,
>
> I have a little question. I have an array that contains 25 values, and I need to compare each
> value to another value within a query. Previously I had 25 different variables and compared each
> individually, but then with more and more users using our system my session management was not
> working correctly so I was forced to change. Now, with the new change, I am getting the following
> error message:
>
> Parse error: parse error, expecting
T_STRING' or
> T_VARIABLE' or `T_NUM_STRING'
if you do "SELECT ... '$SESSION[" <- this quote closes the string so p101
doesn't make much sense to the parserm and it sais the above.
> My query looks like the following:
>
> $result = mysql_query("SELECT * FROM data WHERE user = '$user' AND (uid =
> '$SESSION["p101"]' OR :
[...]
you may want to do something like:
$s = $SESSION; // shorthand, make an alias
and one of the following:
$query_string = sprintf ("SELECT [...] AND (uid='%s' OR uid='%s'
[...]",
$s['p101'], $s['p102'] [...]);
$result = mysql_query ($query_string);
or, considering your query, use the IN (...) SQL condition, and prepare the
SQL query in two steps, a bit more allaborated, but more elegant:
$uid_values = '';
foreach (range (101,125) as $idx) {
$uid_values .= ",'$SESSION[p$idx]'";
}
// chop the first comma (or just add an invalid uid)
$uid_values = substr ($uid_values, 1);
$query_string = "SELECT * FROM data WHERE user='$user' AND uid IN ($uid_values)
[...]";
> It is very long and tedious, and the problem lies in the calls to the array values $SESSION.
> Does anyone know if you can use an array in this manner, or so I have to convert each array
> value into a stand alone variable then compare from here?
you can also do that with extract();
-- teodor