Forum Moderators: coopster

Message Too Old, No Replies

Simple Search Of Database

Adding error response and improving database performance

         

oceanwave

2:21 pm on Jan 27, 2005 (gmt 0)

10+ Year Member



I have a form where a person enters their username and email address and it is sent to the following php page using GET (I simplified it here):

<html>
<head>
</head>
<body bgcolor="blue">
<br><br>
<h2>Your Information</h2></center><b>
<?php
include("dbinfo.php");

$sql = "SELECT * FROM table WHERE username='$username' AND email='$email'";
$query = mysql_query($sql) or die("Cannot query the database.<br>" . mysql_error());
while($result = mysql_fetch_array($query)) {
$id = stripslashes($result["id"]);
$filename = $result["filename"];
$category = $result["category"];

echo "<br>Your Information:<br><br> $category<br>$id<br>$filename<br>";
}
?>
</body>
</html>

Question 1: If no records are found that match the user's input, just "Your Information" is displayed on the next page. I would want to tell the user that no records were found for that username and email. Searching Google last night, the code examples I found were all if/else statements that didn't seem to work for me. How is this written and where is it inserted?

Question 2: Considering that this may become a very large database, and only 1 record is pulled, should my sql statement be:
"SELECT * FROM table WHERE username='$username' AND email='$email' LIMIT 1";
Would this be less taxing on the database? I only want one match, so once the match is found is the search stopped?

Question 3: Which brings me to me final question. Last night I saw on some examples that there were commands to stop a search once an item was found. Do I need this kind of command? If I do, what is it and where is it inserted?

Thanks so much for your help!

[edited by: oceanwave at 2:58 pm (utc) on Jan. 27, 2005]

elgumbo

2:57 pm on Jan 27, 2005 (gmt 0)

10+ Year Member



For Question 1: you could do a simple check to see if there are any results in the record set.

Maybe something like this will work. (untested)

<?php
include("dbinfo.php");

$sql = "SELECT * FROM table WHERE username='$username' AND email='$email'";
$query = mysql_query($sql) or die("Cannot query the database.<br>" . mysql_error());

while($result = mysql_fetch_array($query)) {
$id = stripslashes($result["id"]);
$filename = $result["filename"];
$category = $result["category"];

//check if record set is empty
if (mysql_num_rows($query) < 1) {
echo "<br />No Records here buddy<br />";
}else{

echo "<br>Your Information:<br><br> $category<br>$id<br>$filename<br>";
}
//Don't forget to end the ELSE
}
//OK

?>

Not sure about questions 2/3 though

oceanwave

3:53 pm on Jan 27, 2005 (gmt 0)

10+ Year Member



Thanks for replying elgumbo!

I tried your suggestion (which looks correct to me) and the same thing happened as last night when I entered incorrect information into the 2 fields....I just get a page with "Your Information" at the top, nothing else. I am working with a red background now, so I thought maybe the message was printed in red. I changed the background color, still no message. By the way, when I enter correct information I do get a correct response on the next page. I'm baffled!

I am also still curious about questions 2 & 3.

oceanwave

5:49 pm on Jan 27, 2005 (gmt 0)

10+ Year Member



Got it to work...don't know why it didn't work before. Had to be added at the end like this:

echo "<br>Your Information:<br><br> $category<br>$id<br>$filename<br>";
}
if (mysql_num_rows($query) < 1) {
echo "<b>Entry not found in database!</b><br>";
}
?>
</body>
</html>

Thanks again elgumbo!

I'm still wondering about questions 2 & 3?