开发者

How can I retrieve a column value from a table into a PHP variable?

This is my current code:

$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

This is where I'm getting stuck, as I am getting an "Undefined index" error.

As I am always expecting only one record ($id is unique), if I do this instead:

if (is_array($img开发者_StackOverflowRow))
    $scode = $imgRow[0]; //

I see that $scode is "Array", which is NOT the value that is in the "shortcode" column for that row.

Any pointers?


Even though it returns one record, I suspect it is still doing so as a multidimensional array, where each row has a numeric index (even if it's just one row at [0]) and columns are indexed by name. Try instead:

if (is_array($imgRow))
   $scode = $imgRow[0]["shortcode"];

Always use print_r() or var_dump() to examine the structure of your arrays and objects when debugging.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜