Re: global variable declaration

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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)

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux