开发者

Access object attribute with variable?

Not sure if the title is exactly what i want to do. Below is my working code.

        mysql_select_db($_POST[database]);
        $table_list = mysql_query('SHOW TABLES');
        $tables_in_db = "Tables_in_" . $_POST[database];
        while ($row = mysql_fetch_object($table_list)) {
            echo 开发者_开发百科"<tr>
                    <td class='pageBody'>" . $row->$tables_in_db . "</td>
                  </tr>";
        }

Is it possible to remove line 4 and access the object attribute with $row->Tables_in_{$_POST[database]} in some manner? I've tried a couple different ways including various placing of quotes and curly braces, i had to resort to assigning the whole attribute to the variable $tables_in_db and then use that variable to access the attribute.


You have the right idea, just not the proper syntax. Variable properties can either take the form of a single variable ($a->$b) or some other expression that returns a string containing the name of the property ($a->{'b'}).

$row->{'Tables_in_' . $_POST['database']}

Aside: using one of the other mysql_fetch_* functions, knowing the column name would become irrelevant. For example, mysql_fetch_array() or mysql_fetch_row() and accessing the $row[0] item.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜