Dan Shirah wrote:
Okay, I know this is probably a real easy fix, and I could swear I've done
it before, but for some reason it's just not working.
Below is my query, it does a conditional search based on info put in a form
by the user. If I put in a valid letter/name for the last_name that I know
is in my database everything works fine.
BUT, if I put in a character/name for the last_name that does not exist in
my database I get the following error: Call to undefined function
mssql_error() when I should be getting the echo, "No results"
The snipet of my code that is throwing the error is:
if (!empty($tot_result)) {
$total_results = mssql_num_rows($tot_result) or die(mssql_error());
}
Should I not use (!empty()) when checking to see if a query did not return
any results?
MY CODE:
<?php
// Figure out the total number of results in DB:
$sql_total= "SELECT * FROM payment_request WHERE payment_request.status_code
= 'P'";
if ($customer_last != "") {
$sql_total.=" AND last_name LIKE '$customer_last%'";
if ($customer_first != "") {
$sql_total.=" AND first_name LIKE '$customer_first%'";
}
}
$tot_result = mssql_query($sql_total) or die(mssql_error());
Fist off, mssql_error() is not a valid function... You should be using
mssql_get_last_message() instead.
But, read on first...
You have a couple choices here.
if ( $tot_result ) {
or
if ( is_resource($tot_result) ) {
or (my favorite)
<?php
#####
# Remember to clean $customer_last & $customer_first before you
# place them in your SQL statement. Unfortunately, mssql_* doesn't have
# a function for cleaning your values before you us them. You will need
# to create your own method of input validation here.
#
# Also, I'm assuming that since you didn't show your mssql_connect() call,
# that you are only showing us part of your script...
#####
// Figure out the total number of results in DB:
$sql_total= "SELECT * FROM payment_request WHERE payment_request.status_code =
'P'";
if ($customer_last != "") {
$sql_total .= " AND last_name LIKE '{$customer_last}%'";
}
if ($customer_first != "") {
$sql_total .= " AND first_name LIKE '{$customer_first}%'";
}
if ( ( $tot_result = mssql_query($sql_total) ) === false ) {
echo 'MSSQL ERROR: '.mssql_get_last_message();
} else {
$total_results = mssql_num_rows($tot_result);
if ( $total_results == 0 ) {
echo 'No results found.';
} else {
// Figure out the total number of pages. Always round up using
ceil()
$total_pages = ceil($total_results / $max_results);
echo "Results per page: {$max_results}<br />\n";
echo "Total results: {$total_results}<br />\n";
echo "Total number of pages needed: {$total_pages}<br />\n";
}
}
?>
This should work for you...
if (empty($tot_result)) {
echo "No results";
}
if (!empty($tot_result)) {
$total_results = mssql_num_rows($tot_result) or die(mssql_error());
}
// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);
//echo "Results per page: ".$max_results."\n <br />";
//echo "Total results: ".$total_results."\n <br />";
//echo "Total number of pages needed: ".$total_pages."\n <br />";
?>
--
Jim Lucas
"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."
Twelfth Night, Act II, Scene V
by William Shakespeare
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php