#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    UK
    Posts
    14
    Rep Power
    0
    hi ..

    ihave setup a query which works fine, but i need to be able the print "no results found" to the screen if the query produces no results.. i have tried using the "die" command but this just gives me no results found all the time

    thanks

    Jonny.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Posts
    63
    Rep Power
    14
    You can use:

    if($whatever = mysql_query("SELECT ....")) {
    (code to display results)
    } else {
    print("no result found");
    }
  4. #3
  5. .Net Developer
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    London
    Posts
    987
    Rep Power
    15

    <<
    but i need to be able the print "no results found" to the screen if the query produces no results.. i have tried using the "die" command but this just gives me no results found all the tim
    >>


    You may try this:

    $result=mysql_query("SELECT * FROM tblname");

    if(mysql_num_rows($result)>0){
    //records are their...
    //display result..

    }else{
    echo "no Resultsn";
    exit;
    }


    ------------------
    SR -
    webshiju.com
    www.jobxyz.com-IT Career Portal
    ezipindia.com--WebStudio


    "The fear of the LORD is the beginning of knowledge..."

    [This message has been edited by Shiju Rajan (edited September 14, 2000).]
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    UK
    Posts
    14
    Rep Power
    0
    hi chaps..

    i have tried your code, but still no luck

    i have tried it a different way, but get the "No Results Found" on every search

    my code:

    <?php

    // Request the text of all the links

    $query = mysql_query("SELECT * FROM freeware WHERE title LIKE '%$searchall%' AND platform = '$platform' ORDER BY title ASC");

    $result = mysql_query($query);
    $row = @mysql_fetch_array($result);

    if(!$row)
    {
    echo "No Results Found";
    exit();
    }else{

    // Display the text of each link in a paragraph

    while ($row = @mysql_fetch_array($query))
    {
    $finfo = $row["fileinfo"];
    $title = $row["title"];
    $ftype = $row["filetype"];
    $auth = $row["author"];
    $plat = $row["platform"];
    $des = $row["description"];
    $size = $row["size"];
    $link = $row["link"];

    // Code to layout the results
    }
    }
    ?>

    ------------------
    -----------------------------------
    www.elitewebdesigns.co.uk/
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Posts
    63
    Rep Power
    14
    Change

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    $query = mysql_query("SELECT * FROM freeware WHERE title LIKE '%$searchall%' AND platform = '$platform' ORDER BY title ASC");

    $result = mysql_query($query);
    [/code]

    to just

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    $result = mysql_query("SELECT * FROM freeware WHERE title LIKE '%$searchall%' AND platform = '$platform' ORDER BY title ASC");
    [/code]

    When you use $somevar = mysql_query(some query), that will actually perform the query. If you want to use a seperate variable for the query text, you would need to use

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    $query = "SELECT.....";
    $result = mysql_query($query);
    [/code]

    Also change

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    if(!$row)
    [/code]

    to

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    if(!$result)
    [/code]

    The reason for this is that when php does a mysql_query and it doesn't find any data, it will return 0.

    [This message has been edited by chris22 (edited September 15, 2000).]
  10. #6
  11. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    Colchester, England
    Posts
    131
    Rep Power
    14
    Try This:

    <?php

    // Request the text of all the links

    $db = mysql_connect("localhost", "user");
    mysql_select_db("myDatabseName",$db);

    $result = mysql_query("SELECT * FROM freeware WHERE title LIKE '%$searchall%' AND platform = '$platform' ORDER BY title ASC",$db);

    if ($myrow = mysql_fetch_array($result))
    {

    do
    {
    //display results
    }
    while ($myrow = mysql_fetch_array($result));
    }
    else
    {

    // no records to display

    echo "Sorry, no records were found!";

    }


    // Code to layout the results
    }
    }
    ?>

    Andy J

    [This message has been edited by eaamj01 (edited September 16, 2000).]
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    UK
    Posts
    14
    Rep Power
    0
    cheers eaamj01

    your script worked a treat..

    thanx to everyone for their help

    phew :]

    Jonny.



    ------------------
    -----------------------------------
    www.elitewebdesigns.co.uk/
  14. #8
  15. No Profile Picture
    Apprentice Deity
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 1999
    Location
    Niagara Falls (On the wrong side of the gorge)
    Posts
    3,237
    Rep Power
    19
    Just a correction to Chris22's comment:

    <quote>
    The reason for this is that when php does a mysql_query and it doesn't find any data, it will return 0.
    </quote>

    This is not correct. mysql_query returns a 0 on an error, NOT on an empty result set.

  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Posts
    63
    Rep Power
    14
    You're right. I should have seen that.

    No more posting after staying up till 4 in the morning.

Similar Threads

  1. Need advice designing a usage tracking database (MySQL)
    By Randolpho in forum Database Management
    Replies: 0
    Last Post: February 13th, 2004, 04:11 PM
  2. Newbie needing help sending data to mysql database
    By blackface in forum PHP Development
    Replies: 3
    Last Post: February 13th, 2004, 12:26 AM
  3. Replies: 3
    Last Post: February 11th, 2004, 08:30 AM
  4. Replies: 14
    Last Post: February 8th, 2004, 01:28 PM
  5. MYSQL database
    By isheikh in forum MySQL Help
    Replies: 3
    Last Post: January 14th, 2004, 10:58 PM

IMN logo majestic logo threadwatch logo seochat tools logo