2011-07-27 51 views
0

我有一些問題類型的對話在此代碼(與Facebook PHP SDK 3.0.1工作):PHP SQL查詢錯誤

$page_id = 192485754113829; 
$post_limit = 2; 

$query = "select post_id, source_id, actor_id, target_id, created_time, likes, message, attachment, comments from stream where source_id = '.$page_id.' LIMIT '.$post_limit.'"; 

Parser error: unexpected '.' at position 130.

我無法解釋,這個代碼不在不同的託管中以相同的方式工作。 有沒有在php.ini某種類型的設置

+3

當調試像這樣的問題時,步驟#1:'echo $ query;' – JJJ

回答

4
$query = "select post_id, source_id, actor_id, target_id, 
created_time, likes, message, attachment, comments 
from stream where source_id = '$page_id' LIMIT $post_limit "; 

會工作。

或者,如果你想使用級聯:

$query = "select post_id, source_id, actor_id, target_id, 
created_time, likes, message, attachment, comments 
from stream where source_id = ".$page_id." LIMIT ".$post_limit; 
+0

這是我的錯誤。 正確的不工作代碼是: ''queries'=>'{「stream」:「select source_id ='。$ page_id',其中source_id = 「 LIMIT'。$ post_limit。'「}'' – Sergey

1
$query = "select post_id, source_id, actor_id, target_id, created_time, likes, message, attachment, comments from stream where source_id = '$page_id' LIMIT $post_limit"; 

$query = "select post_id, source_id, actor_id, target_id, created_time, likes, message, attachment, comments from stream where source_id = '".$page_id."' LIMIT ".$post_limit.""; 

(如果page_id是一個整數,你可以把它寫不帶任何引號)

問題是在引號。

0

這是一個語法錯誤。它不適用於任何主機。

1

它與類型轉換無關,只是你的引號是錯誤的。該代碼應該是這樣的:

$page_id = 192485754113829; 
$post_limit = 2; 

$query = "select post_id, source_id, actor_id, target_id, created_time, likes, message, attachment, comments from stream where source_id = ".$page_id." LIMIT ".$post_limit.""; 
+0

$ post_limit之後的引號會減少:) – niktrs

0

你以爲你做字符串連接,但你不知道。這些點位於雙引號字符串中,因此沒有特殊含義。它們在SQL語句中無效。

作爲變量在雙引號的字符串被解析,你可以這樣做:

"select (...) where source_id = $page_id LIMIT $post_limit"; 

如果你想使用字符串連接,你有正確終止字符串:

'select (...) where source_id = ' . $page_id . ' LIMIT ' . $post_limit; 

Read more about strings.

0

您基本上正在生成此查詢(爲了更好地顯示,我已將其重新格式化):

select post_id, source_id, actor_id, target_id, created_time, likes, 
    message, attachment, comments 
from stream 
where source_id = '.1.9248575411383E+14.' LIMIT '.2.' 

這絕不是有效的SQL。首先,你必須擺脫額外的點。

除了你的無效SQL,192485754113829是非常大的整數。最大尺寸爲integers取決於平臺,但在我的電腦中,限制爲2^32,比您的號碼小。這意味着PHP將把它作爲浮點數處理,你將失去精度。鑑於它是一個ID,我建議你改用PHP字符串。