Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
if/else failing when query produces zero results
Old 01-04-2008, 12:00 PM if/else failing when query produces zero results
Junior Talker

Posts: 1
Trades: 0
This one is a baffler fer sure. Scenario:

I have this query (code below) that has the two main variables (category and loan_type) set by the search form. The search works fine and produces the results as long as both variables find a match. The problem is when the second variable has no match...then the script just fails. A blank page is all that is produced.

The two 'AND' statements represent those search requirements. If I purposefully search for an item that I know doesn't have results then the blank page appears. If I search for items known to be positive results then it works fine.

I also tried changing the 2nd 'AND' statement to 'OR' which then produced results without incident. However, the 'OR' is not the conditions we want as it produces results if either condition one OR condition two are met. But that experiment shows that the query works...just not when trying to meet both AND conditions when there's no matching results.

Search code (guts of it anyway):

Code:
// query for extracting lenders from amember database
$category = strip_tags(trim($_POST['category']));
$loantype = strip_tags(trim($_POST['loan_type']));
//$loanamount = strip_tags(trim($_POST['loan_size']));

$sql = "SELECT * FROM amember_members WHERE is_lender='Yes' AND '$category' IN (category_1, category_2, category_3, category_4, category_5) AND '$loantype' IN (loan_type, loan_type2, loan_type3, loan_type4, loan_type5) ORDER BY top_3 LIMIT 10";

$results = mysql_query($sql) or die(mysql_error());
$numrows = mysql_num_rows($results) or die(mysql_error());

if ($numrows === 0){
    include 'header.php';
    echo "<h3>No Results Found</h3><br>
    <p class='bodytext'>No lenders matched your search criteria. Try modifying the search parameters for broader results.<br>\n";
    echo "<br><input type=button value=\"Return to Search\" onClick=\"history.go(-1)\">";
    include 'footer.php';
} else {
include 'header.php';
echo "<h3>The following lenders meet your search criteria.</h3><br>\n";
    // create display of results in abbreviated format
    while ($row = mysql_fetch_array($results)) {
        echo "
        
        <div class='wrapper_out' onmouseover=\"this.className='wrapper'\"; onmouseout=\"this.className='wrapper_out'\";>
        <table width='500' border='0' align='center'>
        <tr ><td colspan='2' class='lendertitle'>".$row['company_name']."</td></tr>
        <tr><td colspan='2' class='lenderdesc'>Company phone: ".$row['company_phone']."</td></tr>
        <tr><td width='300' class='lenderdesc'>Company contact: ".$row['company_contact']."</td>
        <td width='200'><a class='lenderlink' href='loanrequest.php?lenderid=".$row['member_id']."'>Submit Loan Request</a></td></tr>
        <tr><td colspan='2'>";
        
        echo pullRating($row['member_id'],true,false,true);
        echo "</td></tr>
        </table></div><br />\n";
        echo "<hr style='border-top: 1px dotted #666666' color='#666666' width='400' size='1'>";
    }
include 'footer.php';
}
The 'if' statement checking results should divert to the 'No results found..' message if there's no matches. Instead it just results in a completely blank page which indicates the script is failing or not completing. Viewing page source shows nothing. So it's not outputting anything at all in the event there's no matches.

If you want to see this: http://www.lenderfish.com/search-test.php

In first dropdown select anything. In 2nd one select anything BUT 'Stated' and you'll see results and that the search works. Go back to the search and then select 'Stated' in the 2nd box and try again. Blammo. Blank page. It fails to find a match because there aren't any members that match the 'Stated' loan type.

Ideas? Help?
simcomedia is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-04-2008, 01:00 PM Re: if/else failing when query produces zero results
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
I'd suggest rewriting your code a bit. This should output any errors.
PHP Code:
// query for extracting lenders from amember database
$category strip_tags(trim($_POST['category']));
$loantype strip_tags(trim($_POST['loan_type']));
//$loanamount = strip_tags(trim($_POST['loan_size']));

$sql "SELECT * FROM amember_members WHERE is_lender='Yes' AND '$category' IN (category_1, category_2, category_3, category_4, category_5) AND '$loantype' IN (loan_type, loan_type2, loan_type3, loan_type4, loan_type5) ORDER BY top_3 LIMIT 10";

$results mysql_query($sql) or die(mysql_error());

include 
'header.php';

if (
$results && mysql_num_rows($results) > 0) {
    echo 
"<h3>The following lenders meet your search criteria.</h3><br>\n";
    
// create display of results in abbreviated format
    
while ($row mysql_fetch_array($results)) {
        echo 
"
        
        <div class='wrapper_out' onmouseover=\"this.className='wrapper'\"; onmouseout=\"this.className='wrapper_out'\";>
        <table width='500' border='0' align='center'>
        <tr ><td colspan='2' class='lendertitle'>"
.$row['company_name']."</td></tr>
        <tr><td colspan='2' class='lenderdesc'>Company phone: "
.$row['company_phone']."</td></tr>
        <tr><td width='300' class='lenderdesc'>Company contact: "
.$row['company_contact']."</td>
        <td width='200'><a class='lenderlink' href='loanrequest.php?lenderid="
.$row['member_id']."'>Submit Loan Request</a></td></tr>
        <tr><td colspan='2'>"
;
        
        echo 
pullRating($row['member_id'],true,false,true);
        echo 
"</td></tr>
        </table></div><br />\n"
;
        echo 
"<hr style='border-top: 1px dotted #666666' color='#666666' width='400' size='1'>";
    }
} else {
  echo 
"<h3>No Results Found</h3><br>
    <p class='bodytext'>No lenders matched your search criteria. Try modifying the search parameters for broader results.<br>\n"
;
    echo 
"<br><input type=button value=\"Return to Search\" onClick=\"history.go(-1)\">";    
    
    if (!empty(
mysql_error())) {
      echo 
'<h3>MySQL Error</h3><p>'.mysql_error().'</p>';
    }
}
include 
'footer.php'
Also, you may want to change

PHP Code:
$category strip_tags(trim($_POST['category']));
$loantype strip_tags(trim($_POST['loan_type'])); 
to

PHP Code:
$category mysql_real_escape_string(strip_tags(trim($_POST['category'])));
$loantype mysql_real_escape_string(strip_tags(trim($_POST['loan_type']))); 
for added protection against SQL injection.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 01-04-2008, 03:41 PM Re: if/else failing when query produces zero results
Foundationflash's Avatar
Ultra Talker

Posts: 410
Name: Harry Burt
Location: Colchester, Essex, England
Trades: 0
The PHP site isn't very clear about what mysql_num_rows returns if no records are found - your symptoms seem to suggest that it may return false rather than 0 - thus "no results" would return neither 0 nor more than 0;

It would be a simple change to flick it to == instead of ===, so it might be worth a try.
__________________
Foundation Flash tutorials :
Please login or register to view this content. Registration is FREE


New Dreamed Up Web Design:
Please login or register to view this content. Registration is FREE
Foundationflash is offline
Reply With Quote
View Public Profile Visit Foundationflash's homepage!
 
Reply     « Reply to if/else failing when query produces zero results
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.54715 seconds with 12 queries