Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php select中的mysql查询出错_Php_Mysql_Select - Fatal编程技术网

Php select中的mysql查询出错

Php select中的mysql查询出错,php,mysql,select,Php,Mysql,Select,我有一个问题,我想对搜索表单数据进行查询,在这里人们插入一些过滤器将搜索产品,在这个特定的情况下出现较少的是房屋。 使用或不使用该选项进行查询它只会生成字段,而且不起作用,因为我不希望用户填写所有字段,他们希望填写的字段也是如此,所以我这样做了 $texto = array(); $contador=0; if($_SESSION["finalidade"]!=""){ $contador++; $texto[1]="`finalidade` LIKE ".$finalidad

我有一个问题,我想对搜索表单数据进行查询,在这里人们插入一些过滤器将搜索产品,在这个特定的情况下出现较少的是房屋。 使用或不使用该选项进行查询它只会生成字段,而且不起作用,因为我不希望用户填写所有字段,他们希望填写的字段也是如此,所以我这样做了

$texto = array();
$contador=0;

if($_SESSION["finalidade"]!=""){
    $contador++;
    $texto[1]="`finalidade` LIKE ".$finalidade;}

if($_SESSION["tipoimovel"]!=""){
    $contador++;
    $texto[2]="`tipoimovel` LIKE ".$tipoimovel;}

if($_SESSION["nquarto"]!=0){
    $contador++;
    $texto[3]="`nquartos` = ".$nquarto;}

if($_SESSION["pmin"]!=0){
    $contador++;
    $texto[4]="`preco` > ".$pmin;}

if($_SESSION["pmax"]!=0){
    $contador++;
    $texto[5]="`preco` < ".$pmax;}

if($_SESSION["elevador"]!=""){
    $contador++;
    $texto[6]="`elevador` LIKE ".$elevador;}

if($_SESSION["garagem"]!=""){
    $contador++;
    $texto[7]="`garagem` LIKE ".$garagem;}

if($_SESSION["vistapriv"]!=""){
    $contador++;
    $texto[8]="`vistapreveligiada` LIKE ".$vistapriv;}

if($_SESSION["piscina"]!=""){
    $contador++;
    $texto[9]="`piscina` LIKE ".$piscina;}

if($_SESSION["jardim"]!=""){
    $contador++;
    $texto[10]="`jardim` LIKE ".$jardim;}

if($_SESSION["condominiof"]!=""){
    $contador++;
    $texto[11]="`condominio` LIKE ".$condominiof;}

if($_SESSION["conselho"]!=""){
    $contador++;
    $texto[12]="`conselho` LIKE ".$conselho;}

if($_SESSION["frequesia"]!=""){
    $contador++;
    $texto[13]="`frequesia` LIKE ".$frequesia;}

if($_SESSION["refimovel"]!=""){
    $contador++;
    $texto[14]="`referencia` LIKE ".$refimovel;}

    $textocompleto="";


$contador2=1;

for($y = 0; $y < 16; $y++) {

    if($texto[$y]!=""){
        if($contador2==1 && $contador2!=$contador){
            $contador2++;
            ;
        $textocompleto=$texto[$y]." AND ";  }
        elseif($contador2==$contador){
            $contador2++;

        $textocompleto=$textocompleto.$texto[$y];}
        elseif($contador2==1 && $contador2==$contador){
            $contador2++;

        $textocompleto=$texto[$y];  }
        else {
            $contador2++;

        $textocompleto=$textocompleto.$texto[$y]." AND ";}



    }



}

$results = $mysqli->prepare("SELECT ID, imagem, frequesia ,conselho, preco FROM `imovel` WHERE ?  ORDER BY `imovel`.`preco` DESC LIMIT ?,? ");
$results->bind_param("sii",$textocompleto,  $page_position, $item_per_page);
$results->execute();
$results->bind_result($id ,$imagem, $frequesia ,$conselho, $preco); //bind variables to prepared statement
} 
问题是我不能返回任何东西,不能向我显示任何价值。 已经不知道还能做什么,因为如果你为每一个可能性做一个if,将会给出超过100个if,并且需要很长时间。
如果有人知道一些有效且快速的方法来解决此问题,请帮助我,因为实际上不知道如何解决此问题,谢谢

您不能绑定SQL语句的一部分,您只能绑定值

所以这是有效的:

$results = $mysqli->prepare("SELECT col1, col2 FROM tbl WHERE col3 = ?");
$results->bind_param("i", 1234);
这不是:

$results = $mysqli->prepare("SELECT col1, col2 FROM tbl WHERE ?");
$results->bind_param("s", "col3 = 1234");
但是,您可以连接字符串:

prepare("SELECT ... WHERE ".$textocompleto." ORDER BY imovel.preco DESC LIMIT ?,? ");

我想您需要将这些值包装在单个qoute中

例如:

$texto[1]="`finalidade` LIKE ".$finalidade;
应该是

$texto[1]="`finalidade` LIKE '".$finalidade."'";

试试这个。。我认为它会解决这个问题

您可以在$\\u会话变量上使用前缀,如sf{variable\u name}作为所有必填字段。之后,您可以在它们之间循环以构建查询 ps:未测试的代码:

好的,在一些测试之后,我在一个针对媒体的测试数据库中对PDO对象做了同样的操作。PDO几乎和MYSQLI对象一样,它对我来说工作得很好,只是根据您的情况调整您的db ID

$pdo = new PDO('mysql:host=localhost;dbname=media_center', 'root', 'root');

$_SESSION["sf_title"] = "Interstellar";
$_SESSION["sf_description"] = "sci-fi";

$sql = "SELECT * ";
$sql_cond = " WHERE ";
$texto = [];

foreach($_SESSION as $k => $v){
    if(substr($k, 0, 3) == "sf_"){
        $name = substr($k, 3, strlen($k));
        $texto[$name] = $v;
        $sql_cond .= $name." LIKE :".$name." OR ";
    }
}

$sql_cond = substr($sql_cond, 0, strlen($sql_cond)-4);
$final_sql = $sql." FROM media ".$sql_cond;
$results = $pdo->prepare($final_sql);

foreach($texto as $k => $v){
    $key = ":".$k;
    $value = "%".$v."%";
    $results->bindParam($key, $value);
}

$results->execute();

首先,你开始上课了吗?那么你有2倍的有效期?但在你的束缚中有三个。这里没有检查错误。请参考以下链接,并将其应用到代码中。另外,您不能将列绑定到何处?检查错误会告诉你这一点。这就是它失败的原因。是的,我开始会话$muda=$_会话[muda];我有3个?哪里限度在prepareSELECT上进行回显。。。使用绑定变量和将输出放入SQL数据库中。这就是如何得到正确的错误消息。试试看。啊,我害怕的是。。。你知道这是怎么回事吗?如果$finalidade是一个整数,那么他们的代码是有效的/正确的。@IgorPonte你之前没有回答一个重要的问题。$finalidade的价值是多少?它是字符串还是整数?字符串=城市整数=123@IgorPonte然后他们的答案是错误的,我从一开始就感觉到了。你把单个qoutes放在所有条件下了吗?是的,然后去完成你要填充的度量。代码给我一个错误致命错误:调用成员函数在非对象上执行。你能帮我吗?你能放这个吗代码在我的代码里,请让我看看我做错了什么?
$pdo = new PDO('mysql:host=localhost;dbname=media_center', 'root', 'root');

$_SESSION["sf_title"] = "Interstellar";
$_SESSION["sf_description"] = "sci-fi";

$sql = "SELECT * ";
$sql_cond = " WHERE ";
$texto = [];

foreach($_SESSION as $k => $v){
    if(substr($k, 0, 3) == "sf_"){
        $name = substr($k, 3, strlen($k));
        $texto[$name] = $v;
        $sql_cond .= $name." LIKE :".$name." OR ";
    }
}

$sql_cond = substr($sql_cond, 0, strlen($sql_cond)-4);
$final_sql = $sql." FROM media ".$sql_cond;
$results = $pdo->prepare($final_sql);

foreach($texto as $k => $v){
    $key = ":".$k;
    $value = "%".$v."%";
    $results->bindParam($key, $value);
}

$results->execute();