1

Here is my code

I am searching from Mysql database and want to show message if record found status is FOUND else if record not found display FOUND??

<?php
    $status="";
    if ($_SERVER["REQUEST_METHOD"] == "POST")
    {   
        $degree=$_POST["degree"];
        $mysql_host = "*******";
        $mysql_database = "**********";
        $mysql_user = "*******";
        $mysql_password = "*********";

        $conn = mysql_connect($mysql_host, $mysql_user, $mysql_password);
        if(!$conn )
        {
            die('Could not connect: ' . mysql_error());
        }

        mysql_select_db($mysql_database)or die("cannot select DB");
        $sql = "SELECT *
            FROM studentRecord
            WHERE degree='$degree'";
        $result=mysql_query($sql,$conn);
        if(!$result )
        {
            $status="not found";
        }
        else
        {
            while($row = mysql_fetch_array($result))
            {    
                echo  $row['name'];echo "<br>";
                echo  $row['degree'];
                echo "<br>";
            }
            $status="";
        }
        echo "<h1>" . $status . "</h1>";    
    }
?>

here is my form

<h1>Search</h1>
<form action="search.php" method="post">
    <div class="form_settings">
        <p>
            <select name="degree">
                <option></option>
                <option>Civil Engineering</option>
                <option>Urban & Infrastructure Engineering</option>
                <option>Petroleum Engineering</option>
                <option>Mechanical Engineering</option>
                <option>Textile Engineering</option>
                <option>Industrial & Manufacturing Engineering</option>
                <option>Automotive & Marine Engineering</option>
                <option>Electrical Engineering</option>
                <option>Computer & Information Systems Engineering</option>
                <option>Electronic Engineering</option>
                <option>Chemical Engineering</option>
                <option>Materials Engineering</option>
                <option>Metallurgical Engineering</option>
                <option>Polymer & Petrochemical Engineering</option>
                <option>Software Engineering</option>
                <option>Construction Engineering</option>
                <option>Computer Science & Information Technology</option>
            </select>
            <span style="float:right;"class="error">
        </p>
        <br>
        <input class="submit" type="submit" name="searchSubmit" value="SEARCH" />  
    </div>    
</form>

I am searching from Mysql database and want to show message if record found status is FOUND else if record not found display FOUND??

4
  • ...and what isn't working on your code? Commented Jan 19, 2014 at 17:31
  • 1
    ps, might want to cover up your username and password... Commented Jan 19, 2014 at 17:35
  • $status not showing when record not found Commented Jan 19, 2014 at 18:47
  • solved @nigro.simone has solved it Commented Jan 19, 2014 at 18:59

1 Answer 1

2

use mysql_num_rows()

$result = mysql_query($sql, $conn) or die( mysql_error() );

if( mysql_num_rows($result) <= 0 )
    echo 'not found';
else
    echo 'found';

PS. mysql_* is deprecated, use mysqli_* or pdo

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.