当php的mysql_query返回0时,会产生错误。 怎么修?

when the user doesnt have any tags to display, query returns : Undefined variable: tags in last line of the function.

what is the best way to handle this error?

i was thinking of doing if $tags == 0 {$tags=''}; else return $tags; but this didnt work.

function show_users_tags($userid){

    $sql="SELECT id, tag_name from tags WHERE user_id='$userid'";
    $result = mysql_query($sql);
    while($data = mysql_fetch_object($result)){
        $tags[] = array(    'tag_name' => $data->tag_name, 
                            'id' => $data->id       
                    );
    }
    return $tags;
}

When no results are found in your query, the while loop won't be executed and the $tags variable will never be written to, causing the notice Undefined variable when returning it.

Put $tags = array(); on top of your function to initialize the variable to an empty array.

write

$tags = array();

outside of while loop.