On the form user enter rego no where it search database and display the output what i am looking is, if the entered data not match its should display " Record not found."

<?php

$search_Rego = ($_POST['RegoNo']);

  try 
 {
    //open the database
    $db = new PDO('sqlite:iSearch.sqlite');


    $result = $db->query("SELECT  Fname , Lname , RegoNo, Model, Color, MobNo, DeskNo, AltMobNo
    FROM  Owner
    JOIN Cars ON Owner.id=Cars.Owner_id 
    JOIN Contacts ON Owner.id=Contacts.Owner_id
    where  RegoNo like '$search_Rego'");

{

    foreach($result as $row)
                {
             echo " This Car Belongs to " ; 
              echo "<br />"."<br/>";
             echo $row['Fname']." ". $row['Lname']."<br />"; 
             echo $row[ 'RegoNo']. "<br/>";
             echo $row['Model']." ". $row['Color'].   "<br />" ;
             echo "<br />";
             echo $row['MobNo'].     "<br />" ;
             echo $row['DeskNo'].    "<br />";
             echo $row['AltMobNo'].  "<br />";


                }

   }

    // close the database connection
        $db = NULL;
  }
  catch(PDOException $e)
  {
    print 'Exception : '.$e->getMessage();     

  }

?>

Recommended Answers

All 5 Replies

Doesn't it work if you just add the following?

if($result)
{
    // Results were found.
    foreach($result as $row)
    {
        echo " This Car Belongs to " ;
        echo "<br />"."<br/>";
        echo $row['Fname']." ". $row['Lname']."<br />";
        echo $row[ 'RegoNo']. "<br/>";
        echo $row['Model']." ". $row['Color']. "<br />" ;
        echo "<br />";
        echo $row['MobNo']. "<br />" ;
        echo $row['DeskNo']. "<br />";
        echo $row['AltMobNo']. "<br />";
    }
}
else
{
    // No results were found.
    echo '<p>No results were found.</p>';
}

Hi minitauros thanks for you quick response i tried that not woking when i enter the rego no if the rego no in database it showing the result . but when the data in not in database or not enterd any data it showing blank page not Displaying that record not found.

Hm yea for as far as I know the PDO::query() method returns a PDO statement (see also here). I don't know SQLite though. What works for me is this:

try
{
    //open the database
    $db = new PDO('sqlite:iSearch.sqlite');

    $statement = $db->prepare("SELECT Fname , Lname , RegoNo, Model, Color, MobNo, DeskNo, AltMobNo
        FROM Owner
        JOIN Cars ON Owner.id=Cars.Owner_id
        JOIN Contacts ON Owner.id=Contacts.Owner_id
        WHERE RegoNo LIKE :search_rego");
    $statement->bindValue(':search_rego', $search_Rego);
    $statement->execute();
    $result = $statement->fetchAll();

    // close the database connection
    $db = NULL;
}
catch(Exception $e)
{
    echo 'Exception : ' . $e->getMessage();
}

if($result)
{
    // Display results
    foreach($result as $row)
    {
        echo ' This Car Belongs to ';
        echo '<br>' . '<br>';
        echo $row['Fname'] . ' ' . $row['Lname'].'<br>';
        echo $row[ 'RegoNo'] . '<br/>';
        echo $row['Model'] . ' ' . $row['Color']. '<br>' ;
        echo '<br>';
        echo $row['MobNo'] . '<br>' ;
        echo $row['DeskNo'] . '<br>';
        echo $row['AltMobNo'] . '<br>';
    }
}
else
{ 
    // No results were found.
    echo '<p>No results were found.</p>';
}

Hey minitauros you are genius . Its working perfectly as i wanted . Great Job . Thanks a lot for your help .

You're welcome :).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.