Re: global variable declaration

From: Date: Wed, 08 Feb 2006 15:01:41 +0000
Subject: Re: global variable declaration
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-230093@lists.php.net to get a copy of this message
suresh kumar wrote:
hai,
       this is my sample code:
                        i am having user table  with SponsorID as one field in mysql  database,this is my query;
        for($j=0;$j<5;$j++):
       $result=mysql_query("select * from user where SponsorID='$id[$j]' ");
      endfor;
        in  above code when i   print  mysql_num_rows($result) inside for loop its output is  5.when i  print outside the for loop its o/p is 0.I searched weberdev.com website  and declared  $result as global.but same output 0.i want $result  variable to be  available outsibe the forloop and  all other  part of my code.i am looking forward suggestions from ur side.
                                                                     A.suresh
               				
--------------------------------- Jiyo cricket on Yahoo! India cricket Yahoo! Messenger Mobile Stay in touch with your buddies all the time.
At first i would rather use the LIMIT from MySQL than a for loop. $query = "SELECT * FROM foo WHERE bar = 'foobar' LIMIT 5"; $result = mysql_query($query); // Now you can use a while loop to print it out while ($row = mysql_fetch_assoc($result)) // mysql_fetch_assoc is the same as mysql_fetch_array with ASSOC {
    echo $row["somefield"];
    // to have it availible out of the code use this
    $outer_array[] = $row;
} print_r($outer_array); // now the array has the fields of the Query in it. // as you can see using print_r(); // now you can access it like: $outer_array[0]; $outer_array[1]; ... $outer_array[n]; Your FOR loop replaces the variable $result everytime. That's why it is empty. Greets Barry -- Smileys rule (cX.x)C --o(^_^o) Dance for me! ^(^_^)o (o^_^)o o(^_^)^ o(^_^o)

« previous php.general (#230093) next »