|
-
Jul 6th, 2002, 05:41 AM
#1
Thread Starter
New Member
Searching a database
This code puts out what I search for fine:
$result = mysql_query("SELECT * FROM mytable WHERE id = $variable");
while($row = mysql_fetch_array($result)){
echo $row["id"];
echo "<br>";
echo $row["name"];
echo "<br>";
}
What I can't figure out is if there is no id $variable in the table, how do I get it to come back and:
echo "nothing found";
I'd guess its some sort of 'if' statement, but I don't know exactly how to construct it in this case.
-
Jul 6th, 2002, 07:46 AM
#2
Fanatic Member
PHP Code:
if ($variable!=NULL) {
$result = mysql_query("SELECT * FROM mytable WHERE id = $variable");
while($row = mysql_fetch_array($result)){
echo $row["id"];
echo "<br>";
echo $row["name"];
echo "<br>";
}
}
Else {
echo ("nothing found");
}
-
Jul 6th, 2002, 09:40 AM
#3
Stuck in the 80s
First off, this isn't searching, this is just pulling out records with matching IDs. Second, there shouldn't be matching IDs.
You would probably also want to do it this way to find out whether or not any records are found. What was posted above will just tell you if there was no variable:
PHP Code:
if ($variable!=NULL) {
$result = mysql_query("SELECT * FROM mytable WHERE id = $variable");
[b]if ($row = mysql_fetch_array($result)) {[/b]
do {
echo $row["id"];
echo "<br>";
echo $row["name"];
echo "<br>";
} while($row = mysql_fetch_array($result));
[b]} else {[/b]
echo "No records found!";
}
} else {
echo "No search variable (id?) defined!";
}
Last edited by The Hobo; Jul 6th, 2002 at 09:43 AM.
-
Jul 6th, 2002, 01:47 PM
#4
Fanatic Member
or you could do
PHP Code:
$result = mysql_query("SELECT * FROM mytable WHERE id = $variable");
if(!$result){
echo 'Not found<br>';
} else{
while($row = mysql_fetch_array($result)){
echo $row['id'] . "<br>" . $row['name'] . "<br>";
}
}
Visit www.fragblast.com
Gaming, forums, and a online RPG/Battle system
(__Flagg) DOT NET? is this a Hindi Dating service?
-
Jul 6th, 2002, 10:23 PM
#5
Stuck in the 80s
I'm still thinking this isn't searching...
and also, why can't we use [b] tags in the [php] tag anymore?
-
Jul 7th, 2002, 08:10 AM
#6
Fanatic Member
Originally posted by The Hobo
[B]I'm still thinking this isn't searching...
and also, why can't we use tags in the [php] tag anymore?
Yah that isnt searching, but o well.
-
Jul 8th, 2002, 03:18 AM
#7
Thread Starter
New Member
Thanks to everyone who replied. As to weather this is considered searching or not, I wouldn't know. I know very little about any of this. The id isn't actually what I'm looking for. That's just what I put in the example. What I am actually doing is taking the id from one table and checking to see if there are any instances of it in a related field in a couple of other tables. At any rate it seems to work for my purposes. Anyone who wants to take the time to show me a better way to go about this would be appreciated.
Thanks
ED
-
Jul 9th, 2002, 06:18 AM
#8
Originally posted by The Hobo
[B]I'm still thinking this isn't searching...
and also, why can't we use tags in the [php] tag anymore?
Do you have enhanced mode on? see the left on your post reply page.
-
Jul 9th, 2002, 04:09 PM
#9
Stuck in the 80s
Originally posted by ricmitch_uk
Do you have enhanced mode on? see the left on your post reply page.
No, but I can't see why that would have anything to do with it?
-
Jul 9th, 2002, 04:10 PM
#10
Stuck in the 80s
PHP Code:
if ($variable!=NULL) {
$result = mysql_query("SELECT * FROM mytable WHERE id = $variable");
[b]if ($row = mysql_fetch_array($result)) {[/b]
do {
echo $row["id"];
echo "<br>";
echo $row["name"];
echo "<br>";
} while($row = mysql_fetch_array($result));
[b]} else {[/b]
echo "No records found!";
}
} else {
echo "No search variable (id?) defined!";
}
-
Jul 9th, 2002, 04:11 PM
#11
Stuck in the 80s
Still doesn't work with "Enhanced Mode."
I swear we used to be able to do it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|