Click to See Complete Forum and Search --> : Searching a database
mutate_ed
Jul 6th, 2002, 05:41 AM
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.
Gimlin
Jul 6th, 2002, 07:46 AM
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");
}
The Hobo
Jul 6th, 2002, 09:40 AM
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:
if ($variable!=NULL) {
$result = mysql_query("SELECT * FROM mytable WHERE id = $variable");
if ($row = mysql_fetch_array($result)) {
do {
echo $row["id"];
echo "<br>";
echo $row["name"];
echo "<br>";
} while($row = mysql_fetch_array($result));
} else {
echo "No records found!";
}
} else {
echo "No search variable (id?) defined!";
}
nabeels786
Jul 6th, 2002, 01:47 PM
or you could do
$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>";
}
}
The Hobo
Jul 6th, 2002, 10:23 PM
I'm still thinking this isn't searching...
and also, why can't we use [b] tags in the [php] tag anymore?
Gimlin
Jul 7th, 2002, 08:10 AM
Originally posted by The Hobo
I'm still thinking this isn't searching...
and also, why can't we use [b] tags in the [php] tag anymore?
Yah that isnt searching, but o well.
mutate_ed
Jul 8th, 2002, 03:18 AM
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
ricmitch_uk
Jul 9th, 2002, 06:18 AM
Originally posted by The Hobo
I'm still thinking this isn't searching...
and also, why can't we use [b] tags in the [php] tag anymore?
Do you have enhanced mode on? see the left on your post reply page.
The Hobo
Jul 9th, 2002, 04:09 PM
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?
The Hobo
Jul 9th, 2002, 04:10 PM
if ($variable!=NULL) {
$result = mysql_query("SELECT * FROM mytable WHERE id = $variable");
if ($row = mysql_fetch_array($result)) {
do {
echo $row["id"];
echo "<br>";
echo $row["name"];
echo "<br>";
} while($row = mysql_fetch_array($result));
} else {
echo "No records found!";
}
} else {
echo "No search variable (id?) defined!";
}
The Hobo
Jul 9th, 2002, 04:11 PM
Still doesn't work with "Enhanced Mode."
I swear we used to be able to do it. :confused:
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.