开发者

Need help on how to convert PHP code from object to procedural?

I was hoping someone can help me convert this piece of code from object form to procedural form.

So I can have a sense of what to do for when I try to convert the larger piece of code that is all in object form that this snippet came from.

I'm a brand new beginner.

$sql = "SELECT id, label, link_url, parent_id FROM dyn_menu ORDER BY parent_id, id ASC";
$items = mysql_query($sql);
while ($obj = mysql_fetch_object($items)) {
    if ($obj->parent_id == 0) {
        $parent_menu[$obj->id]['开发者_StackOverflow社区label'] = $obj->label;
        $parent_menu[$obj->id]['link'] = $obj->link_url;
    } else {
        $sub_menu[$obj->id]['parent'] = $obj->parent_id;
        $sub_menu[$obj->id]['label'] = $obj->label;
        $sub_menu[$obj->id]['link'] = $obj->link_url;
        $parent_menu[$obj->parent_id]['count']++;
    }
}
mysql_free_result($items);

I know there is something wrong with this code?

$mysqli = new mysqli("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT id, label, link_url, parent_id FROM dyn_menu ORDER BY parent_id, id ASC");

if (!$dbc) {
    // There was an error...do something about it here...
    print mysqli_error();
}

while ($obj = mysql_fetch_assoc($dbc)) {
    if ($obj->parent_id == 0) {
        $parent_menu $obj['id']['label'] = $obj['label'];
        $parent_menu $obj['id']['link'] = $obj['link_url'];
    } else {
        $sub_menu $obj['id']['parent'] = $obj['parent_id'];
        $sub_menu $obj['id']['label'] = $obj['label'];
        $sub_menu $obj['id']['link'] = $obj['link_url'];
        $parent_menu $obj['parent_id']['count']++;
    }
}
mysql_free_result($dbc);


I'm not at all sure what you're trying to accomplish. Instead of mysql_fetch_object, you want to use something else? mysql_fetch_assoc and mysql_fetch_array are largely similar. Instead of $obj->id, you'd write $obj['id'].

As for using mysql_result (fetching data one column at a time, by column number), it is supposedly much slower. I can't really think of a good reason to use it.

Update:
Your second code snippet uses a mix of three different ways to access MySQL: mysqli object style, mysqli procedural style, and the 'old' mysql extension (mysql_* functions). That is is not going to work.

There are some decent examples on this PHP Manual page. They show both the object oriented and "procedural" style.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜