[PHP] using mysql_query() twice?

2002-01-11 Thread Erik Price

I am curious if using the same mysql_query() function twice causes some 
kind of problem.

It seems that if I use

$result = mysql_query($sql, $db);
if (mysql_fetch_array($result)) {
 while ($row = mysql_fetch_array($result) or die(You lose.  No data 
fetched.)) {
  // print some data
  // print a simple sentence to see if the WHILE statement even 
executes
 }
} else {
 // print No results match your criteria
}


In this, I have used the $result variable twice.  $result is essentially 
nothing more than a mysql_query() function.  If I do the above, then the 
IF statement is true but the WHILE statement doesn't seem to produce its 
intended effect.  In other words, I get the die() message You lose.  No 
data fetched.

This leads me to believe that I can't run two instances of mysql_query() 
or perhaps mysql_fetch_array().

Does anyone know for sure if this is the case?

Erik


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] using mysql_query() twice?

2002-01-11 Thread Nick Wilson

-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1


* On 11-01-02 at 23:08 
* Erik Price said

 I am curious if using the same mysql_query() function twice causes some 
 kind of problem.
 
 It seems that if I use
 
 $result = mysql_query($sql, $db);
 if (mysql_fetch_array($result)) {
 while ($row = mysql_fetch_array($result) or die(You lose.  No data 
 fetched.)) {
  // print some data
  // print a simple sentence to see if the WHILE statement even 
 executes
 }
 } else {
 // print No results match your criteria
 }
 
 
 IF statement is true but the WHILE statement doesn't seem to produce its 
 intended effect.  In other words, I get the die() message You lose.  No 
 data fetched.

I'll most likely be corrected (I just joined the list to brush up on my
php) but I think that if you change this 

 while ($row = mysql_fetch_array($result) or die(You lose.  No data 

to this

 while ($row = mysql_fetch_array($another_result) or die(You lose.  No data 

You'll be on the right track.


 This leads me to believe that I can't run two instances of mysql_query() 
 or perhaps mysql_fetch_array().

No, of course you can.


HTH
- -- 

Nick Wilson

Tel:+45 3325 0688
Fax:+45 3325 0677
Web:www.explodingnet.com



-BEGIN PGP SIGNATURE-
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE8P2XjHpvrrTa6L5oRArToAJ96owiXpTmPnOGSOI2jzvMHnAwm1ACbBXk2
DbgVDF6NwxSwOA8+p86zNC0=
=0Z6i
-END PGP SIGNATURE-

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] using mysql_query() twice?

2002-01-11 Thread Erik Price


EXCELLENT!
It worked perfect.
So $result can't be used twice -- I wonder if it somehow is altered by 
the mysql_fetch_array() that is performed upon it.

Thanks Nick.

On Friday, January 11, 2002, at 05:23  PM, Nick Wilson wrote:

 I'll most likely be corrected (I just joined the list to brush up on my
 php) but I think that if you change this

 while ($row = mysql_fetch_array($result) or die(You lose.  No data

 to this

 while ($row = mysql_fetch_array($another_result) or die(You 
 lose.  No data

 You'll be on the right track.


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] using mysql_query() twice?

2002-01-11 Thread Nick Wilson

-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1


* On 11-01-02 at 23:39 
* Erik Price said

 
 EXCELLENT!
 It worked perfect.
 So $result can't be used twice -- I wonder if it somehow is altered by 
 the mysql_fetch_array() that is performed upon it.

That's not it. Your first $result is a resource indicator (container for
results) so if you re-assign it (put another value into it) it becomes
useless as your while statement depends on it.

Happy to help :)
- -- 

Nick Wilson

Tel:+45 3325 0688
Fax:+45 3325 0677
Web:www.explodingnet.com



-BEGIN PGP SIGNATURE-
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE8P2pbHpvrrTa6L5oRAqWrAJ0RPOMtsW4IhBTkzKycyJnnZHwATACfTct6
Pi2wOViPXfXlpR1V3iw78Is=
=RpGT
-END PGP SIGNATURE-

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] using mysql_query() twice?

2002-01-11 Thread Erik Price

On Friday, January 11, 2002, at 05:42  PM, Nick Wilson wrote:

 That's not it. Your first $result is a resource indicator (container for
 results) so if you re-assign it (put another value into it) it becomes
 useless as your while statement depends on it.


(This may be redundant, but I'm hoping that someday someone will find 
this info in the archives as helpful:)

INSIGHT!

Now I understand.  I was mistaken in the way that I first conceptualized 
the following:

$sql = some SQL code;
$result = mysql_query($sql, $db);

Originally, I thought that $result was just a variable that represented 
the function mysql_query($sql, $db).  And when $result is called by, 
for instance, mysql_fetch_array(), in the form 
mysql_fetch_array($result), all I was really doing was making a more 
organized form of this:

   mysql_fetch_array((mysql_query($sql, $db)))

But no!  It appears that defining the variable $result like so:

$result = mysql_query($sql, $db);

Is actually performing an operation!  The mysql_query() function happens 
at this time!  Not during the mysql_fetch_array() as I previously 
thought.

If what I have written above here is correct (and I hope it is) then you 
have helped me learn a fundamental element of the way PHP works, at 
least with this command.  I had not been thinking of $result as a 
container for results before, rather as a container for another 
function for later use.

Awesome.

Erik



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]