0

我想sql根据从用户那里收到的数据动态创建查询。

代码:

$test = $_POST['clientData']; //It can be an array of values
count($test); //This can be 2 or 3 or any number depending upon the user input at the client

$query = "select * from testTable where testData = ".$test[0]." and testData = ".$test[1]." and . . .[This would vary depending upon the user input]"

是否有可能实现上述场景。我在这方面相对较新。您的指导会有所帮助。

4

8 回答 8

1

使用准备好的语句:

$query = $dbh->prepare("SELECT * FROM testtable WHERE testData=:test0 and testData=:test1");
$query ->bindParam(':test0', $test0);
$query ->bindParam(':test1', $test0);

$test0 = $test[0];
$test1 = $test[1];

$query->execute();
于 2013-10-23T12:45:38.783 回答
1

利用:

<?php

$test=$_POST['clientData'];//It can be an array of values

$query = "select *from testtable where 1 ";
foreach($test as $value) {
    $query .= " AND testData='" . $value . "'";
}

echo $query;

?>
于 2013-10-23T12:46:38.847 回答
1

Rishi 这是一个很长的章节。

如果要搜索单个字段,则可以尝试执行以下操作:

<?php
$test = $_POST[ 'clientData' ];
if( is_array( $test ) ){
    $select = implode( ",", $test );
} else { 
    $select = $test;

}

$query=select *from testtable where testData IN ( $select );
?>

这仅对特定字段的搜索有效。如果你想在多个字段上创建搜索,那么你需要做更多的工作,拥有一个可以创建关系变量名称的关联映射 -> field_to_search

于 2013-10-23T12:48:42.370 回答
0
$q="select *from table where ";
    $a=count($test)-1;
    $b=0;
    while($element = current($test)) {
        $key=key($array);
        if($b!=$a){
            $q.=$key."=".$test[$key]." and ";
        }
        else {
            $q.=$key."=".$test[$key];
        }
        next($array);
        $b=$b+1;
    }

为此,您的数组必须包含columnname作为键,例如

$test['name'],$test['lastname']

然后它会return

$q="select * from table where name=testnamevalue and lastname=testlastnamevalue";

希望它有效

于 2013-10-23T13:00:14.253 回答
0
$test=$_POST['clientData'];//It can be an array of values
    $dValuesCount = count($test);//This can be 2 or 3 or any number depending upon the user input at the client

    $query="select *from testtable ";  


    if ($dValuesCount > 0 ){
        $query .= " WHERE ";

        for ($dCounter = 0; $dCounter <= $dValuesCount ; $dCounter++){

            $query .= "testData=" . $test[$dCounter];
            if ($dCounter != ($dValuesCount - 1)){

            $query .= " AND ";
            }

        }


    }
于 2013-10-23T12:55:22.250 回答
0

这是非常伪代码,因为我真的不知道 PHP,但你能不能做这样的事情

$query = "select * from testable";

$count = count($test);

if($count  > 0)
{ 
    $query .= " where ";

    for ($x=0; $x<=$count; $x++)
    {
        if($x > 0)
        {
              $query .= " and ";
        }

        $query .= " testData='" . $test[x] . "'";
    } 
}
于 2013-10-23T12:44:57.450 回答
0
$test=$_POST['clientData'];
$query="select * from testtable where testData='".$test[0]."' and testData='".$test[1]."' and . . .[This would vary depending upon the user input]";
$result = mysql_query($query);
于 2013-10-23T12:46:20.987 回答
0
$data = $_POST['data'];

$query = "SELECT";

if ( is_set($data['columns']) )
   $query .= " ".implode(',',$data['columns']);
else
   $query .= "*";

if ( is_set($data['table']) )
$query .= " ".$data['table'];

和 ...

于 2013-10-23T12:50:08.347 回答