2014-03-19 57 views
-3

我正在嘗試執行基於先前查詢中獲取的值的查詢。類似的東西:PHP:將變量轉換爲字符串查詢

$variableid = 100; 

$query_prev = "SELECT query FROM queries_table WHERE id = 1"; 
$result_prev = pg_query($pg,$query_prev); 
$row_prev = pg_fetch_array($result_prev); 


$final_query = $row_prev['query']; 

$row_prev['query'] value would be "SELECT * FROM other_table WHERE id = $variableid"; 

$final_query value at this point is: "SELECT * FROM other_table WHERE id = $variableid" 

/* but that I want is this value: */ "SELECT * FROM other_table WHERE id = 100" 
+1

你在數據庫中存儲SQL? **爲什麼**? –

+2

我最好的建議是不要做這個,它是一個可怕的想法。無論你想要實現什麼(不清楚你的問題),相信我還有更好的辦法。 – scav

+0

重要的是馬克b嗎? – fMac

回答

0

使用if語句之前,你做下選擇這樣:

<?php if ($row_prev['query']= 100){ 

    SELECT .......... etc. 
    }else 
    SELECT from other 

    ?> 

/希望這是你想要的

+0

非常感謝Ria但不是,eval就是我一直在尋找的東西。 – fMac

0

解決:

$variableid = 100; 

$query_prev = "SELECT query FROM queries_table WHERE id = 1"; 
$result_prev = pg_query($pg,$query_prev); 
$row_prev = pg_fetch_array($result_prev); 
$query = $row_prev['query']; 

eval("\$final_query = \"$query\";");