Forum Moderators: coopster
<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]
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
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.
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?