通过php搜索MYSQL数据检索

时间:2014-08-07 07:29:18

标签: php mysql

我正在尝试从mysql中检索一些数据。用户应输入许可证编号并单击搜索,并显示与该编号相关的所有记录。不幸的是,当我输入许可证号码并按搜索时,不显示任何内容。请参阅下面的代码,看看我出错了。

<?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>';

        }

    }

}

?>

1 个答案:

答案 0 :(得分:0)

我最终完成了这项工作。只需编写一组新代码即可。我只需要应用格式化的abit,以便输出整齐。

      <?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;
}
 ?>