php数据库问题

I have written this PHP code and worked correctly... then I wanted a particular code segment to be worked as a function, but as soon I did this I didn't get the correct result... I'm confused what has gone wrong, can somebody please help me with this... Thanks a lot...

Here's the code gives me the error...

$arr1=array();

$date = date("D");

$link = mysql_connect ('localhost', 'root', ''); 

$db = mysql_select_db ('dayevent', $link);

function grabData($arr){ //works properly NOT as a function, but I want to make this code part act like a funciton
     $i=0;
     $sql = "SELECT event FROM events WHERE day = '$date'"; 
     $sel = mysql_query($sql); 

     echo $sel; //this prints Resource id #3 
     if (mysql_num_rows($sel) > 0) { // but doesn't go into if block

          while($row = mysql_fetch_array($sel)) { 
               echo $row['event'] . '<br />'; 

               //storing DB query result in array   

               $arr[$i]=$row['event'];

               $i=$i+1;     

          } 

          foreach($arr as $key => $value) {
               echo $key . " " . $value . "<br />";

          }

     } else echo 'Nothing returned!'; //prints this instead of the correct result

}

grabData($arr1);

mysql_close(); 

Move this inside your function: $date = date("D");. The way it is now, $date is not defined. If you run with error_reporting(E_ALL) you would have caught it right away.

Test Below Code :

EDITED

$arr1=array();
$date = date("D");

$link = mysql_connect ('localhost', 'root', ''); 
$db = mysql_select_db ('dayevent', $link);

function grabData()
{
    global $link,$date;

    $arr = array();
    $i=0;
    $sql = "SELECT event FROM events WHERE day = '$date'"; 
    $sel = mysql_query($sql,$link); 

    echo $sel; //this prints Resource id #3 
    if (mysql_num_rows($sel) > 0)
    {
        while($row = mysql_fetch_array($sel)) 
        { 
            echo $row['event'] . '<br />'; 
            $arr[$i]=$row['event'];
            $i=$i+1;    
        } 
        foreach($arr as $key => $value)
        {
            echo $key . " " . $value . "<br>";
        }
    } else echo 'Nothing returned!'; //prints this instead of the correct result
    return $arr;
}

print_r( grabData() );
mysql_close();