Page 1 of 1

Problem showing results

Posted: Sat Sep 27, 2003 9:19 am
by Linkjames
Ok, the following code should search from a database, find an record, display the question, either with or without the answer, or if there is no result say as much. It works for showing the question with or without the answer, but if there are no results, nothing shows up.

Code: Select all

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<? // First part of script time script ?>
<?PHP include("includes/exetime1.php"); ?>
<body>
<?PHP
//Connect to MySql
include("includes/connect.php");
//Select Database
include("includes/dbselect.php");
//Sets search string as a variable
$search = $_POST["search"];
//Searchs database for the asked question
$result = @mysql_query("SELECT * FROM search WHERE question LIKE '$search'");
//Checks that the query executed succesfully or show error message
include("includes/queryerror.php");
if (mysql_num_rows($result) == 1) { 
// Start while loop
while ( $row = mysql_fetch_array($result) ) {
$answered = $row["answered"];
$question = $row["question"];
$answer = $row["answer"];
if ($answered == 0) {
echo $question;
echo('<P>');
echo ('Your Qustion has not yet been answered<P>');
// End answered == 0 if
}
else if ($answered == 1) {
echo $question;
echo('<P>');
echo $answer;
echo('<P>');
// End answered == 0 else if
}
// End if there are results
}
// Start if no results
if (mysql_num_rows($result) != 1) { 
echo ('Sorry you question was not found<P>');
// End if no results
}





// End while
}
?>


<? // Second part of script time script ?>
<?PHP include("includes/exetime2.php"); ?>
</body>
</html>

Posted: Sat Sep 27, 2003 9:30 am
by Linkjames
Stupidly it was the closing bracket of my while statement that was screwing up the code
Working code is

Code: Select all

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<? // First part of script time script ?>
<?PHP include("includes/exetime1.php"); ?>
<body>
<?PHP
//Connect to MySql
include("includes/connect.php");
//Select Database
include("includes/dbselect.php");
//Sets search string as a variable
$search = $_POST["search"];
//Searchs database for the asked question
$result = @mysql_query("SELECT * FROM search WHERE question LIKE '$search'");
//Checks that the query executed succesfully or show error message
include("includes/queryerror.php");
if (mysql_num_rows($result) == 1) { 
// Start while loop
while ( $row = mysql_fetch_array($result) ) {
$answered = $row["answered"];
$question = $row["question"];
$answer = $row["answer"];
if ($answered == 0) {
echo $question;
echo('<P>');
echo ('Your Qustion has not yet been answered<P>');
// End answered == 0 if
}
else if ($answered == 1) {
echo $question;
echo('<P>');
echo $answer;
echo('<P>');
// End answered == 0 else if
}
// End while
}
// End if there are results
}
// Start if no results
if (mysql_num_rows($result) != 1) { 
echo ('Sorry you question was not found<P>');
// End if no results
}






?>


<? // Second part of script time script ?>
<?PHP include("includes/exetime2.php"); ?>
</body>
</html>