这是我当前的代码:

$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()来检查数组和对象的结构。

10-05 19:25