2013-10-23 44 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"; 

希望工程