2013-10-23 146 views
0

我想动态创建sql查询,具体取决于我从用户接收到的数据。动态创建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]" 

是否有可能实现上述方案。我在这方面比较新,你的指导将会有所帮助。

+1

我想这是'PHP'并没有那么多'SQL'有关? –

+0

是的,我通过PHP发射sql查询。只是添加了PHP tag.Thanks。 – Ravikanth

+0

$ test是一个数组? –

回答

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; 

?> 
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] . "'"; 
    } 
} 
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(); 
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); 
1

Rishi这是一个非常lon g章节。

如果你想搜索到一个单一的领域,那么你可以尝试做:

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

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

和...

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

     } 


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

希望工程