Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Dec 2009
    Posts
    102
    Thanks
    18
    Thanked 0 Times in 0 Posts

    shoulnt this work?

    this script shows results from a search , im trying to add a if else statement to let the users know if no matches was found
    shouldnt the code work? when a search matches it shows the fields from the table but when nothing match it doesnt show the else statement

    thanks Kimbo




    <body>
    <?php

    $con = mysql_connect("localhost","xxxx","xxxxx");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }



    mysql_select_db("xxxxxx", $con);




    $search =check($_POST['search'], "you havent enterd anything , <a href=personal.php>go back</a> ");




    function check($data, $error='')

    {

    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($error && strlen($data) == 0) {
    die($error);
    }
    return $data;
    }




    $search=$_POST["search"];


    $result = mysql_query("SELECT * FROM personal WHERE name LIKE '%$search%'");


    while($r=mysql_fetch_array($result))
    {


    $name=$r["name"];
    $adress=$r["adress"];
    $areacode=$r["areacode"];
    $phonenumber=$r["phonenumber"];
    $email=$r["email"];
    $id=$r["id"];

    if($result){ //works

    echo "$name <br> $adress <br> $area code <br> $phonenumber<br />
    $email <br>";
    }

    else {
    echo "no records was found"; //dont work
    }




    }

    ?>

  • #2
    met
    met is offline
    Regular Coder
    Join Date
    Oct 2009
    Location
    United Kingdom
    Posts
    728
    Thanks
    4
    Thanked 119 Times in 119 Posts
    the reason it doesn't work is that providing a valid SQL query is provided, mysql_query will return true, regardless of results.

    even if no results are found, it's still "true", so the else condition never occurs.

    [snip, misread your code]

    PHP Code:
    $result mysql_query("SELECT * FROM personal WHERE name LIKE '%$search%'");
    $num_results=mysql_num_rows($result);

    /* get results.. */

    if($num_results>0) { // there are results..

        
    while($r=mysql_fetch_array($result))
        {
                
    $name=$r["name"];
                
    $adress=$r["adress"];
                
    $areacode=$r["areacode"];
                
    $phonenumber=$r["phonenumber"];
                
    $email=$r["email"];
                
    $id=$r["id"];
                echo 
    "$name <br> $adress <br> $area code <br> $phonenumber<br /> $email <br>";
        } 
    // while

    } else {

        echo 
    "no records was found"//dont work


    Last edited by met; 05-16-2010 at 08:34 PM.

  • Users who have thanked met for this post:

    kimboslice (05-16-2010)

  • #3
    Regular Coder
    Join Date
    Dec 2009
    Posts
    102
    Thanks
    18
    Thanked 0 Times in 0 Posts
    thanks alot

  • #4
    Regular Coder
    Join Date
    Dec 2009
    Posts
    102
    Thanks
    18
    Thanked 0 Times in 0 Posts
    it returns else statement now both when the record doesnt match and when it does . any suggestions?

  • #5
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Post your latest code.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #6
    Regular Coder
    Join Date
    Dec 2009
    Posts
    102
    Thanks
    18
    Thanked 0 Times in 0 Posts
    thanks, current code:




    <div >
    <?php

    include ('db_connect.php');

    $search = check($_POST['search'], "you didnt enter anything, <a href=personal.php>go back</a> ");




    function check($data, $error='') {

    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($error && strlen($data) == 0) / {

    die($error);
    }
    return $data;
    }




    $search=$_POST["search"];


    $result = mysql_query("SELECT * FROM personal WHERE name LIKE '%$search%'");

    if($num_results>0)

    while($r=mysql_fetch_array($result))
    {


    $namn=$r["name"];
    $adress=$r["adress"];
    $postnummer=$r["areacode"];
    $telefonnummer=$r["phonenumber"];
    $email=$r["email"];
    $id=$r["id"];



    echo "$name<br> $adress <br> $areacode<br> $phonenumber<br />
    $email <br>";

    }
    else {

    echo "no records was found";

    }



    ?>

  • #7
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Looks like you missed this line
    PHP Code:
    $num_results=mysql_num_rows($result); 
    It goes just after $result.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • Users who have thanked _Aerospace_Eng_ for this post:

    kimboslice (05-17-2010)

  • #8
    Regular Coder
    Join Date
    Dec 2009
    Posts
    102
    Thanks
    18
    Thanked 0 Times in 0 Posts
    thanks to many hours in front of the computer


  •  

    Posting Permissions

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