|
Posted by Good Man on 08/29/07 16:57
giloosh <giloosh99@gmail.com> wrote in news:1188403377.477061.290770
@o80g2000hse.googlegroups.com:
> Would i just loop through the rows that were checked and add them to
> the WHERE STATEMENT using the OR condition?
>
> select * from rows where id = 3 or id = 2 or id = 6 or id = 10
>
> so basically i would do something like this with the php code.
> $sql = "select * from rows WHERE 1 ";
> foreach ($_POST[checkbox] as $check){
> $sql .= " OR id = $check";
> }
>
i'll assume this code is here for brevity, and that you're really making
sure that your $check variable contains what you're expecting it to (a
number).
I tend to craft my SQL 'where' statements AFTER checking for variables,
assembling them as a string, and appending them to a query.
ie:
$whereVar = "1";
foreach($_POST['checkbox'] as $check) {
if(is_numeric($check)) {
$whereVar .= " OR id='$check'";
}
}
$sql = "SELECT * FROM rows WHERE $whereVar";
ps: naming a MySQL table "rows" can certainly lead to confusion down the
road... try to stay away from reserved words and their permutations
entirely
> I just think its slow to use so many OR conditions in one sql
> statement, but i cant think of anyway else to do this.
As long as "id" is properly indexed, it won't be slow. You're searching
for results based on conditions... there's no way to avoid searching!
Navigation:
[Reply to this message]
|