SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast nixon's Avatar
    Join Date
    May 2003
    Location
    Australia
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    (!$results ) query

    Newbie needs a little help. I am using the bit of script at the bottom here to search a table and return values from rows. Everything works fine but I am just trying to add a:
    if (!$results) echo "bla bla bla";

    I think this is what I should use to add a message if there are no results from the search. Unfortunately everywhere I add it it does not work. Is this what I should be using and if so where should it go? If not what should I try? The script I am using is this:

    //Connect to database server
    $table = $db->prefix("fishdiary");

    if($_POST["submit"])
    {
    $fish = $_POST["caught"];
    $query = "SELECT * FROM $table WHERE caught = '$fish'";
    $result = mysql_query($query);
    while ($row = mysql_fetch_array($result))

    {
    {

    $content .= "

    <table border='1' cellpadding='4' cellspacing='0' style='border-collapse: collapse' align=center bordercolor='#FFFFFF' width='90%'>

    <tr>
    <td width='100%' colspan='4' align=center><u><b><font color=darkblue size=2>Submitted by <a href=\"javascript:openWithSelfMain('".XOOPS_URL."/pmlite.php?send2=1&amp;to_userid=".$uid."', 'pmlite', 450, 380);\"><u>$row[uname]</u></a> on the $row[date]</font></b></u></td>
    </tr>
    <tr>
    <td width='50%' colspan='2' align=center bgcolor=B3D6F2><u>Last New Moon:</u> <b>$row[newmoon]</b></td>
    <td width='50%' colspan='2' align=center bgcolor=B3D6F2><u>Days after last New Moon:</u> <b>$row[moonday]</b></td>
    </tr>
    <tr>
    <td width='30%' bgcolor=EAFBFD><u>Started Fishing:</u> <b>$row[beginfishtime]</b></td>
    <td width='40%' colspan='2' bgcolor=EAFBFD><u>Start Fishing Tide:</u> <b>$row[beginfishtide]</b></td>
    <td width='30%' bgcolor=EAFBFD><u>Temperature:</u> <b>$row[peaktemp]</b></td>
    </tr>
    <tr>
    <td width='30%' bgcolor=EAFBFD><u>Stopped Fishing:</u> <b>$row[stopfishtime]</b></td>
    <td width='40%' colspan='2' bgcolor=EAFBFD><u>Stop Fishing Tide:</u> <b>$row[stopfishtide]</b></td>
    <td width='30%' bgcolor=EAFBFD><u>Water Temperature:</u> <b>$row[watertemp]</b></td>
    </tr>
    <tr>
    <td width='30%' bgcolor=EAFBFD><u>Best Fishing Time:</u> <b>$row[bestfishtime]</b></td>
    <td width='40%' colspan='2' bgcolor=EAFBFD><u>Peak Fishing Tide:</u> <b>$row[bestfishtide]</b></td>
    <td width='30%' bgcolor=EAFBFD><u>Barometer:</u> <b>$row[barometer]</b></td>
    </tr>
    <tr>
    <td width='50%' colspan='2' align=center bgcolor=B3D6F2><u>Fish Targetted:</u> <b>$row[target]</b></td>
    <td width='50%' colspan='2' align=center bgcolor=B3D6F2><u>Fish Caught:</u> <b>$row[caught]</b></td>
    </tr>
    <tr>
    <td width='100%' colspan='4' bgcolor=D0FBFD><u>Location:</u><br>
    <b>$row[location]</b></td>
    </tr>
    <tr>
    <td width='100%' colspan='4' bgcolor=D0FBFD><u>Successful Bait:</u><br>
    <b>$row[bait]</b></td>
    </tr>
    <tr>
    <td width='100%' colspan='4' bgcolor=D0FBFD><u>Details of Trip:</u><br>
    <b>$row[details]</b></td>
    <tr>
    <td width='100%' colspan='4'>&nbsp;<hr></td>
    </tr>
    </tr>";
    }
    $content .= "</table>";
    }
    }
    Any help appreciated :)

  2. #2
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    North East of England
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am not sure what you are after. If you want to display the message when your query returns no results you could use

    Code:
    If mysql_num_rows($results <1) {
    
       echo "blablabla";
    
    } else {
    
      // add content
    
    }
    I think by using your

    Code:
    If (!$results) {
    
    }
    this will only occur when there is an error doing the query.

  3. #3
    SitePoint Enthusiast imedia's Avatar
    Join Date
    Oct 2004
    Location
    new england, usa
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was trying the same thing for debugging, if (!$results). No luck.
    Then I found this to work great:

    Code:
    if mysql_num_rows($results) {
    echo $msgResults
    }else{
    echo $msgNoResults
    }
    I don't believe the "<1" is need because, if $results is empty "false" is automatically returned.

  4. #4
    $this->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Dec 2003
    Location
    Federal Way, Washington (USA)
    Posts
    1,524
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by LeeS
    Code:
    If mysql_num_rows($results <1) {
    
       echo "blablabla";
    
    } else {
    
      // add content
    
    }
    Just a point of picky-ness, but the syntax is incorrect for your "if" statement. It should be:
    Code:
    If (mysql_num_rows($results) < 1) {
    Someone else mentioned that if (!$results) didn't work. That's because you always have a result set, which is what that is saying, unless you messed up and have an error in the way you set up $results.

    Another way of doing this would be:
    Code:
    $row_count = mysql_num_rows($results);
    
    if ($row_count ==0) {
    
       echo "blablabla";
    
    } else {
    
      // add content
    
    }
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    Showcase your music collection on the Web

  5. #5
    SitePoint Enthusiast nixon's Avatar
    Join Date
    May 2003
    Location
    Australia
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks everyone, shall mess around with all of them and see how it goes.

  6. #6
    SitePoint Enthusiast nixon's Avatar
    Join Date
    May 2003
    Location
    Australia
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have tried a few variations of the offers but without the result. I either get the "bla bla bla" result correct but none of the identifiers below or the opposite. I tried with $results but kept getting errors so changed to $result. Below is what I have at the moment (this one presents the "bla bla bla" correctly if there is no result, but does not present the data when there is a result).
    //Connect to database server
    $table = $db->prefix("fishdiary");

    if($_POST["submit"])
    {
    $fish = $_POST["caught"];
    $query = "SELECT * FROM $table WHERE caught = '$fish'";
    $result = mysql_query($query);
    while ($row = mysql_fetch_array($result))
    $row_count = mysql_num_rows($result);

    if ($row_count ==0) {

    echo "bla bla bla";

    }else{

    $content .= "

    <table border='1' cellpadding='4' cellspacing='0' style='border-collapse: collapse' align=center bordercolor='#FFFFFF' width='90%'>

    <tr>
    <td width='100%' colspan='4' align=center><u><b><font color=darkblue size=2>Submitted by <a href=\"javascript:openWithSelfMain('".XOOPS_URL."/pmlite.php?send2=1&amp;to_userid=".$uid."', 'pmlite', 450, 380);\"><u>$row[uname]</u></a> on the $row[date]</font></b></u></td>
    </tr>
    <tr>
    <td width='50%' colspan='2' align=center bgcolor=B3D6F2><u>Last New Moon:</u> <b>$row[newmoon]</b></td>
    <td width='50%' colspan='2' align=center bgcolor=B3D6F2><u>Days after last New Moon:</u> <b>$row[moonday]</b></td>
    </tr>
    <tr>
    <td width='30%' bgcolor=EAFBFD><u>Started Fishing:</u> <b>$row[beginfishtime]</b></td>
    <td width='40%' colspan='2' bgcolor=EAFBFD><u>Start Fishing Tide:</u> <b>$row[beginfishtide]</b></td>
    <td width='30%' bgcolor=EAFBFD><u>Temperature:</u> <b>$row[peaktemp]</b></td>
    </tr>
    <tr>
    <td width='30%' bgcolor=EAFBFD><u>Stopped Fishing:</u> <b>$row[stopfishtime]</b></td>
    <td width='40%' colspan='2' bgcolor=EAFBFD><u>Stop Fishing Tide:</u> <b>$row[stopfishtide]</b></td>
    <td width='30%' bgcolor=EAFBFD><u>Water Temperature:</u> <b>$row[watertemp]</b></td>
    </tr>
    <tr>
    <td width='30%' bgcolor=EAFBFD><u>Best Fishing Time:</u> <b>$row[bestfishtime]</b></td>
    <td width='40%' colspan='2' bgcolor=EAFBFD><u>Peak Fishing Tide:</u> <b>$row[bestfishtide]</b></td>
    <td width='30%' bgcolor=EAFBFD><u>Barometer:</u> <b>$row[barometer]</b></td>
    </tr>
    <tr>
    <td width='50%' colspan='2' align=center bgcolor=B3D6F2><u>Fish Targetted:</u> <b>$row[target]</b></td>
    <td width='50%' colspan='2' align=center bgcolor=B3D6F2><u>Fish Caught:</u> <b>$row[caught]</b></td>
    </tr>
    <tr>
    <td width='100%' colspan='4' bgcolor=D0FBFD><u>Location:</u><br>
    <b>$row[location]</b></td>
    </tr>
    <tr>
    <td width='100%' colspan='4' bgcolor=D0FBFD><u>Successful Bait:</u><br>
    <b>$row[bait]</b></td>
    </tr>
    <tr>
    <td width='100%' colspan='4' bgcolor=D0FBFD><u>Details of Trip:</u><br>
    <b>$row[details]</b></td>
    <tr>
    <td width='100%' colspan='4'>&nbsp;<hr></td>
    </tr>
    </tr>";
    }
    $content .= "</table>";
    }
    Any more ideas??

  7. #7
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    You have the num_rows inside your loop..
    PHP Code:
    if($_POST["submit"]) 

    $fish $_POST["caught"]; 
    $query "SELECT * FROM $table WHERE caught = '$fish'"
    $result mysql_query($query);
    $row_count mysql_num_rows($result);

    if (
    $row_count ==0) {

    echo 
    "bla bla bla";

    }else{

    while (
    $row mysql_fetch_array($result))



    $content .= "
    #etc ... 
    Try this!
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  8. #8
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    also just noticed a syntax error,
    PHP Code:
    if($_POST["submit"]) 

    $fish $_POST["caught"]; 
    $query "SELECT * FROM $table WHERE caught = '$fish'"
    $result mysql_query($query); 
    $row_count mysql_num_rows($result); 

    if (
    $row_count ==0) { 

    echo 
    "bla bla bla"

    }else{ 

    while (
    $row mysql_fetch_array($result)) {

    $content .= "

    <table border='1' cellpadding='4' cellspacing='0' style='border-collapse: collapse' align=center bordercolor='#FFFFFF' width='90%'>

    <tr>
    <td width='100%' colspan='4' align=center><u><b><font color=darkblue size=2>Submitted by <a href=\"javascript:openWithSelfMain('"
    .XOOPS_URL."/pmlite.php?send2=1&amp;to_userid=".$uid."', 'pmlite', 450, 380);\"><u>$row[uname]</u></a> on the $row[date]</font></b></u></td>
    </tr>
    <tr>
    <td width='50%' colspan='2' align=center bgcolor=B3D6F2><u>Last New Moon:</u> <b>
    $row[newmoon]</b></td>
    <td width='50%' colspan='2' align=center bgcolor=B3D6F2><u>Days after last New Moon:</u> <b>
    $row[moonday]</b></td>
    </tr>
    <tr>
    <td width='30%' bgcolor=EAFBFD><u>Started Fishing:</u> <b>
    $row[beginfishtime]</b></td>
    <td width='40%' colspan='2' bgcolor=EAFBFD><u>Start Fishing Tide:</u> <b>
    $row[beginfishtide]</b></td>
    <td width='30%' bgcolor=EAFBFD><u>Temperature:</u> <b>
    $row[peaktemp]</b></td>
    </tr>
    <tr>
    <td width='30%' bgcolor=EAFBFD><u>Stopped Fishing:</u> <b>
    $row[stopfishtime]</b></td>
    <td width='40%' colspan='2' bgcolor=EAFBFD><u>Stop Fishing Tide:</u> <b>
    $row[stopfishtide]</b></td>
    <td width='30%' bgcolor=EAFBFD><u>Water Temperature:</u> <b>
    $row[watertemp]</b></td>
    </tr>
    <tr>
    <td width='30%' bgcolor=EAFBFD><u>Best Fishing Time:</u> <b>
    $row[bestfishtime]</b></td>
    <td width='40%' colspan='2' bgcolor=EAFBFD><u>Peak Fishing Tide:</u> <b>
    $row[bestfishtide]</b></td>
    <td width='30%' bgcolor=EAFBFD><u>Barometer:</u> <b>
    $row[barometer]</b></td>
    </tr>
    <tr>
    <td width='50%' colspan='2' align=center bgcolor=B3D6F2><u>Fish Targetted:</u> <b>
    $row[target]</b></td>
    <td width='50%' colspan='2' align=center bgcolor=B3D6F2><u>Fish Caught:</u> <b>
    $row[caught]</b></td>
    </tr>
    <tr>
    <td width='100%' colspan='4' bgcolor=D0FBFD><u>Location:</u><br>
    <b>
    $row[location]</b></td>
    </tr>
    <tr>
    <td width='100%' colspan='4' bgcolor=D0FBFD><u>Successful Bait:</u><br>
    <b>
    $row[bait]</b></td>
    </tr>
    <tr>
    <td width='100%' colspan='4' bgcolor=D0FBFD><u>Details of Trip:</u><br>
    <b>
    $row[details]</b></td>
    <tr>
    <td width='100%' colspan='4'>&nbsp;<hr></td>
    </tr>
    </tr>"
    ;
    }
    $content .= "</table>";

    Try this instead!

    SpikeZ
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  9. #9
    SitePoint Enthusiast nixon's Avatar
    Join Date
    May 2003
    Location
    Australia
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I thought I had tried that but obviously not.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •