Drarok
Drarok

Reputation: 3809

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

When using mysql_fetch_object() to return objects from 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()?

Upvotes: 11

Views: 5410

Answers (3)

Xupypr MV
Xupypr MV

Reputation: 935

Use quotes. Example: "name with spaces"

Upvotes: -3

Pekka
Pekka

Reputation: 449385

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.

Upvotes: 20

Drarok
Drarok

Reputation: 3809

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.

Upvotes: 5

Related Questions