Kayls
nOObee
Joined: 10 May 2008
Posts: 2
66 Cash
|
 Trouble with getting number of rows
I keep getting this error message and I am unsure what is causing it:
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in C:\Program Files\xampp\htdocs\results.php on line 88
Page 88 is referring to the number of rows.
The code is below
<?php
// Check that the form has been submitted
if ($_POST) {
//Store the form data in variables
$form_library_id = $_POST['library_id'];
$form_earliest_date = $_POST['earliest_date'];
$form_latest_date = $_POST['latest_date'];
if (array_key_exists('etype', $_POST)) {
$form_exhibit_type = $_POST['etype'];
}
else {
$form_exhibit_type = "";
}
//Trim any trailing and leadin spaces from the form data
$form_library_id = trim($form_library_id);
$form_exhibit_type = trim($form_exhibit_type);
$form_earliest_date = trim($form_earliest_date);
$form_latest_date = trim($form_latest_date);
//Convert earliest and latest dates to integers
$form_earliest_date = intval($form_earliest_date);
$form_latest_date = intval($form_latest_date);
//Open a connect to the database
$link = mysqli_connect('localhost', 'student', 'mmst12009', 'assignment3');
//Define a query
$query = "
SELECT
libraries.library_name,
exhibits.exhibit_date,
exhibits.exhibit_type,
exhibits.exhibit_description,
exhibits.exhibit_id
FROM libraries, exhibits
WHERE libraries.library_id = exhibits.library_id";
//Restrict the SQL query with an AND clause if the library ID is not 'any'
if ($form_library_id != 'any') {
$query .= " AND libraries.library_id = $form_library_id";
}
//Restrict the SQL query with an AND clause if the library ID is not 'any'
if ($form_exhibit_type != "") {
$query .= " AND exhibits.exhibit_type = '$form_exhibit_type'";
}
//Restrict the SQL query with an AND clause if the library ID is not 'any'
if ($form_earliest_date != 0) {
$query .= " AND exhibits.exhibit_date >= $form_earliest_date";
}
//Restrict the SQL query with an AND clause if the library ID is not 'any'
if ($form_latest_date != 0) {
$query .= " AND exhibits.exhibit_date <= $form_latest_date";
}
//Restrict the SQL query with an AND clause if the library ID is not 'any'
if (exhibits.exhibit_id) {
$query .= " ORDER BY exhibits.exhibit_id";
}
//Run the query and store the link
$result = mysqli_query($link, $query);
//Get the number of rows in the result set
$number_of_rows = mysqli_num_rows($results);
//Close the connection to the database
mysqli_close($link);
//Exit from the script if no records have been retrieved
if ($number_of_rows == 0) {
echo <<<END
<p>There are no matching records in the database or entered dates might be invalid.<br />
Use the BACK button on your Web Browser to return to the previous page and try again. </p>
END;
}
else {
//Display the table headings
echo <<<END
<p>Search results are presented below</p>
<table border="0" width="900" cellpadding="5" cellspacing="5">
<tr>
<th>ID Number</th>
<th>Library</th>
<th>Date</th>
<th>Type</th>
<th>Description</th>
</tr>
END;
//Assign each record in the result to an array
While ($row = mysqli_fetch_array($result)) {
//Assign each array element to a variable
$exhibit_id = $row['exhibit_id'];
$library_name = $row['library_name'];
$exhibit_date = $row['exhibit_date'];
$exhibit_type = $row['exhibit_type'];
$exhibit_description = $row['exhibit_description'];
//Display a table row to each record
echo <<<END
<tr>
<td>$exhibit_id</td>
<td>$library_name</td>
<td>$exhibit_date</td>
<td>$exhibit_type</td>
<td>$exhibit_description</td>
</tr>
END;
}
echo "</table>";
}
}
?>
</body>
</html>
|