View Full Version : "OR" in queries?
Lucky13
May 26th, 2001, 18:55
Hello...I want something like this:
When I go to index.php?id=x or index.php?name=x, if gets the same info for the record, but if I just go to index.php, it does something else. This is what I have:
if ($id OR $name) {
if ($id) {
blah blah FROM database WHILE id=$id
} elseif {
blah blah FROM database WHILE name=$name
}
(this is the individual record)
echo " . $rows[name] . ";
} else {
(this is a list of all of the records)
blah blah
}
Anyone have any ideas? Thank you very much.
~Lucky13
Cheap Bastard
May 27th, 2001, 01:10
wouldn't you just put the HTML page you want displayed with just index.php between the braces of the last else { } statement?
Lucky13
May 27th, 2001, 07:58
That's what I would have through, but it said that the procedure of calling the last query (under "if ($name) {") wasn't supported on my server...
gyrbo
May 27th, 2001, 08:07
useing || I think
Lucky13
May 27th, 2001, 08:09
where?
FROM db WHERE id=$id || name=$name ????
Thanks.
~Lucky13
Cheap Bastard
May 27th, 2001, 10:29
|| is just another way of saying OR
why are you using an elseif statement without giving it anything
either you give it another condition
elseif($name) {
yadayada
}
or you can just make it else
else {
yadayada
}
Also, note if you take the first option you can make it bullet proof by adding another else statement below it. I don't see how anything could get through after your OR statement, but if you're feeling like extra safety take option one. If you're feeling like shorter scripts take option two...
Lucky13
May 27th, 2001, 12:01
OK...it's not working. This is what I have:
if ($id || $name) {
if ($id) {
$result = mysql_query("SELECT * FROM kod WHERE id=$id",$db);
$myrow = mysql_fetch_array($result);
} elseif ($name) {
$result = mysql_query("SELECT * FROM kod WHERE name=$name",$db);
$myrow = mysql_fetch_array($result);
} else {
echo "Unable to comply";
}
yada
} else {
yade
It works fine if I go to Roster.php?id='x', but when I go to Roster.php?name='y' it says that it has a problem with the line that says
$result = mysql_query("SELECT * FROM kod WHERE name=$name",$db);
I'm stuck. Can anyone help?
Thanks
Lucky
gyrbo
May 27th, 2001, 12:10
What is the exact error?
Maybe the colum doesn't exist, or is empty
Lucky13
May 27th, 2001, 12:13
No...the column "name" is there, and it's full. It says
Warning: Supplied argument is not a valid MySQL result resource in /home/niaad/niaad-www/kod/roster.php on line 34
Line 34 is when it call the query with the name=$name stuff.
lucifer
May 27th, 2001, 12:29
2 things
"SELECT * FROM kod WHERE name='$name'",
as it's a text string
put
if ($result=mysql_query(.....)){ do stuff }
else {your error msg}
as returns false if a db error occurs so you can catch errors
Lucky13
May 27th, 2001, 12:35
HORRAY!
Lucifer is smart! You were right about the whole thing with the text string. I'll have to remember that :)
Thanks a lot!
~Lucky13
Powered by vBulletin® Version 4.1.7 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.