开发者

How do you access columns with a space in them via mysql_fetch_object?

When using mysql_fetch_object() to return objects from开发者_StackOverflow社区 a MySQL query, sometimes column names have spaces in them, and cannot be aliased, such as when running SHOW CREATE PROCEDURE. The procedure definition is returned in a column named Create Procedure. In my case, the data abstraction layer only allows the use of mysql_fetch_object(), so I can't simply use mysql_fetch_assoc() to work around this problem.

Can I access columns with spaces in when using mysql_fetch_object()?


Generally speaking,

$recordname->{"my column name"} 

will do the trick.

You can also do a print_r($record); to find out how the columns are represented in the object.


This took me a while to crack, so I thought I'd post it here, hence the quick answer. :)

// Internally, this db abstraction layer uses mysql_fetch_object().
$query = $db->query('SHOW CREATE PROCEDURE `%s`', 'test');
foreach ($query as $row) {
    $procedure = $row->{'Create Procedure'};
}

I don't know if this is specific to a PHP version, but it works under PHP 5.3.1 on Windows, at least.


Use quotes. Example: "name with spaces"

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜