使用PDO在php中查询为空错误

时间:2016-06-20 09:43:57

标签: php pdo phpmyadmin

我收到如下错误: 数组([0] => 42000 [1] => 1065 [2] =>查询为空) 这,在执行以下查询时:     

$servername = "localhost"; //Define the servername here
$username = "admin";       //Define the username here
$password = "";            //Define the password here
$dbname = "data";          //Define the database name here
$x=0;
$construct = null;        //Defining the database variable for Sector     Selection
$Price_value=null;        //Defining the database variable for Price Range
$type_value = null;         //Defining the database variable for Type of Building

$sql = new PDO("mysql:host=$servername; dbname=$dbname", $username, $password);
$sql->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
//Connection to database

foreach($sql->query('SELECT * from data') as $row) {
    print_r($row);
}
echo $dbname;

$dbs = $sql->query('Show databases');                                           //Accessing HTML variable data
$button = (isset($_GET['submit'])?$_GET['submit'] : null);                  
$search = (isset($_GET['search'])?$_GET['search'] : null);
$Type = (isset($_GET['type'])?$_GET['type'] : null);
$Price = (isset($_GET['Price'])?$_GET['Price'] : null);
echo $Type;

if(!$button)
  echo "you didn't submit a keyword";                   //to check whether the submit button was pressed or not
else
{
    echo "you searched for<b>$search</b><hr size = '1'></br>";  
}

$search_exploded = explode(" ", $search);

$construct = "SELECT *FROM data where Sector = $search ";       //Query generation to search according to sector
$sth = $sql->prepare($construct);                               
$sth->bindParam(":search", $search, PDO::FETCH_ASSOC);
$sth->execute();

$sth_type = "SELECT *FROM data where type = $Type  ";       //Query  generated to search according to the Type of Building
$sth_type = $sql->prepare($type_value);
//settype($search, "string");
$sth_type->bindParam(":Type",$Type, PDO::PARAM_STR);
$sth_type->execute();

$sth_Price = "SELECT *from data where Price = $Price && Sector = $search";          //Query generated to search according to the Price Range
$sth_Price = $sql->prepare($Price_value);
$sth_Price->bindParam(":Price",$Price,PDO::PARAM_STR);
$sth_Price->execute();


if($sth_type->execute())
    {
        echo "executed";
    }
else{
        echo "not executed";
        $arr = $sth_type->errorInfo();
        print_r($arr);
    }


$foundnum = 0;

//If loops to check the value of input against the database values


if(strcmp($construct, $search))
    {   
        $foundnum+=1;
        $runrows = $sth->fetchAll(PDO::FETCH_ASSOC);
        echo "executing";
    }
else if((strcmp($construct,$search))&&(strcmp($Type, $type_value)))
    {   
        $foundnum+=1;
        $runrows = $sth_type->fetchAll(PDO::FETCH_ASSOC);
    }

else if((strcmp($construct,$search))&&(strcmp($Type, $type_value))&&(strcmp($Price, $Price_value))) 
    {
        $foundnum+=1;
        $runrows = $sth_Price->fetchAll(PDO::FETCH_ASSOC);
    }
else
    return;

//echo gettype($Price_value);
if($foundnum==0)
echo "Sorry, there are no matching result for '<b>$search</b>'.Try more general words.";
else 
{
 echo "$foundnum results found!<p>";

?>

仔细观察,我发现sth_type和sth_price中的查询没有被执行。这样多个查询无法在单个代码中执行吗?我的基本目标是能够从数据库中提取可以根据用户输入基于查询提取值的行。

0 个答案:

没有答案