[英]How can I retrieve a column value from a table into a PHP variable?
這是我目前的代碼:
$thisImage = "Select * from `posts` where `id`=" . $id;
$imgRow = $d->GetData($thisImage); // returns one record through mysql_get_assoc
$scode = "#"; // init $scode
if (is_array($imgRow))
$scode = $imgRow["shortcode"]; // "shortcode" is the name of a column
這是我遇到困難的地方,因為我收到了“未定義的索引”錯誤。
因為我總是只期待一條記錄($ id是唯一的),如果我這樣做:
if (is_array($imgRow))
$scode = $imgRow[0]; //
我看到$ scode是“Array”,它不是該行“shortcode”列中的值。
有什么指針嗎?
即使它返回一條記錄,我懷疑它仍然是一個多維數組,其中每一行都有一個數字索引(即使它只是[0]
處的一行)並且列是按名稱索引的。 嘗試改為:
if (is_array($imgRow))
$scode = $imgRow[0]["shortcode"];
在調試時,始終使用print_r()
或var_dump()
來檢查數組和對象的結構。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.