简体   繁体   中英

MYSQL Data retrieval through php search

I am trying to retrieve some data from mysql.User is supposed to enter the licence number and click search and all records related to that number should be displayed. Unfortunately when i enter licence number and press search then nothing gets displayed. Please see my code below and see where iam going wrong.

<?php

mysql_connect("localhost", "root", "") or die("cant connect");
mysql_select_db("android_api")or die("cant connect");
$output='';
//collect
if(isset($_POST['search'])){
    $searchq=$_POST['search'];

    //if(isset($_POST['search'])){ to if ($_SERVER["REQUEST_METHOD"] == "POST") {   

    //$query=mysql_query("SELECT * FROM fineregister WHERE licencenum LIKE '%$searchq%'")  or die("cant connect");
    $query = mysql_query("SELECT * FROM fineregister WHERE licencenum LIKE  '%".$searchq."%'");
    $count=mysql_num_rows($query);
    if($count==0){
        $output='no results';
    }else{
        while($row=mysql_fetch_array($query)){
            $fdriver=$row['driver'];
            $flicencenum=$row['licencenum'];
            $fofficer=$row['officer'];
            $fspeed=$row['speed'];
            $ffine=$row['fine'];
            $fcategory=$row['category'];
            $output.='<div> '.$fdriver.' '.$flicencenum.' '.$fspeed.' '.$ffine.'  '.$fcategory.'</div>';

        }

    }

}

?>

I ended up getting this done. Simply wrote a set of new codes and they worked. i Just need to apply abit of formatting so that the output comes out neatly.

      <?php
mysql_connect("localhost", "root", "") or die("Error connecting to database: ".mysql_error());


mysql_select_db("android_api") or die(mysql_error());

?>

 <head>
<title>Search results</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<?php
$query = $_GET['query'];
$min_length = 3;


if(strlen($query) >= $min_length){ 

    $query = htmlspecialchars($query);
    $query = mysql_real_escape_string($query);

    $raw_results = mysql_query("SELECT * FROM register WHERE (`licence` LIKE '%".$query."%')") or die(mysql_error());

    if(mysql_num_rows($raw_results) > 0){ 

        while($results = mysql_fetch_array($raw_results)){
        echo "<p><h3>".$results['driver']." </h3>".$results['licence'].$results['officer'].$results['speed'].$results['fine'].$results['category']."</p>";

        }

    }
    else{ 
        echo "No results";
    }

  }
else{ 
    echo "Minimum length is ".$min_length;
}
 ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM