MS SQL Query question
------=_NextPart_000_21F6F_01C44264.C7994510
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
When I run a query the syntax looks like this:
$result =3D mssql_query($query);
$numRows =3D mssql_num_rows($result);
for($i=3D0; $i<$numRows; $i++)
{
$row =3D mssql_fetch_array($result);
}
but if I go to try and loop through the resultset again it wont work
unless I run this line again "$result =3D mssql_query($query);"
I'm sure this is wasting resources but I can't seem to get around it. =
Any
suggestions?
Thanks,
Ron
------=_NextPart_000_21F6F_01C44264.C7994510--
RE: MS SQL Query question
try something like this
$result = mssql_query ($query);
if ($result) {
while ($template_args = msssql_fetch_array ($res)) {
//what ever you want to do with the data
}
}
Derrick Hermanson
Programmer/Analyst I
Acton International LTD.
402.470.5816
mailto:dhermanson [at] acton.com
-----Original Message-----
From: Ron.Herhuth [at] tatumpartners.com
[mailto:Ron.Herhuth [at] tatumpartners.com]
Sent: Tuesday, May 25, 2004 1:30 PM
To: php-windows [at] lists.php.net
Subject: [PHP-WIN] MS SQL Query question
When I run a query the syntax looks like this:
$result = mssql_query($query);
$numRows = mssql_num_rows($result);
for($i=0; $i<$numRows; $i++)
{
$row = mssql_fetch_array($result);
}
but if I go to try and loop through the resultset again it wont work
unless I run this line again "$result = mssql_query($query);"
I'm sure this is wasting resources but I can't seem to get around it. Any
suggestions?
Thanks,
Ron
--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: MS SQL Query question
What's happening is the pointer on the results is advancing until it
hits the end of the result set. When you run it again, the pointer is
still at the end of the result set and therefore sees nothing 'ahead' of
it.
The same thing can happen with arrays. I'm not sure if you have to do
it still, but I habituatlly do a "reset($arr);" after I do a sort on the
array because the sort did (still does?) leave the array pointer at the
end of the array, necessitating a 'reset'.
Read some of the notes here:
http://us4.php.net/mysql_fetch_array
This one in particular should help:
********************************
Ben
06-Apr-2004 09:59
One of the most common mistakes that people make with this function,
when using it multiple times in one script, is that they forget to use
the mysql_data_seek() function to reset the internal data pointer.
When iterating through an array of MySQL results, e.g.
<?php
while ($line =3D mysql_fetch_array($result, MYSQL_ASSOC)) {
foreach ($line as $col_value) {
echo $col_value . '
';
}
}
?>
the internal data pointer for the array is advanced, incrementally,
until there are no more elements left in the array. So, basically, if
you copy/pasted the above code into a script TWICE, the second copy
would not create any output. The reason is because the data pointer has
been advanced to the end of the $line array and returned FALSE upon
doing so.
If, for some reason, you wanted to interate through the array a second
time, perhaps grabbing a different piece of data from the same result
set, you would have to make sure you call
<?php
mysql_data_seek($result, 0);
?>
This function resets the pointer and you can re-iterate through the
$line array, again!
********************************
-TG
> -----Original Message-----
> From: Derrick Hermanson [mailto:dhermanson [at] acton.com]
> Sent: Tuesday, May 25, 2004 3:05 PM
> To: Ron.Herhuth [at] tatumpartners.com; php-windows [at] lists.php.net
> Subject: RE: [PHP-WIN] MS SQL Query question
>
>
> try something like this
>
> $result =3D mssql_query ($query);
> if ($result) {
> while ($template_args =3D msssql_fetch_array ($res)) {
> //what ever you want to do with the data
> }
> }
>
> Derrick Hermanson
> Programmer/Analyst I
> Acton International LTD.
> 402.470.5816
> mailto:dhermanson [at] acton.com
>
>
> -----Original Message-----
> From: Ron.Herhuth [at] tatumpartners.com
> [mailto:Ron.Herhuth [at] tatumpartners.com]
> Sent: Tuesday, May 25, 2004 1:30 PM
> To: php-windows [at] lists.php.net
> Subject: [PHP-WIN] MS SQL Query question
>
>
>
> When I run a query the syntax looks like this:
>
> $result =3D mssql_query($query);
> $numRows =3D mssql_num_rows($result);
>
> for($i=3D0; $i<$numRows; $i++)
> {
> $row =3D mssql_fetch_array($result);
> }
>
> but if I go to try and loop through the resultset again it wont work
> unless I run this line again "$result =3D mssql_query($query);"
>
> I'm sure this is wasting resources but I can't seem to get
> around it. Any
> suggestions?
>
> Thanks,
> Ron
>
> --
> PHP Windows Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: MS SQL Query question
------=_NextPart_000_22255_01C4426B.E19AF7E0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Trevor,
That is the key!!! You rock...cut my page load time in half.
Thanks my good man!
Ron
>From: Gryffyn, Trevor
>To: Derrick Hermanson; Herhuth, Ron; php-windows [at] lists.php.net
>Sent: 05/25/2004 3:11 PM
>What's happening is the pointer on the results is advancing until it
>hits the end of the result set. When you run it again, the pointer is
>still at the end of the result set and therefore sees nothing 'ahead' of
>it.
>
>The same thing can happen with arrays. I'm not sure if you have to do
>it still, but I habituatlly do a "reset($arr);" after I do a sort on the
>array because the sort did (still does?) leave the array pointer at the
>end of the array, necessitating a 'reset'.
>
>Read some of the notes here:
>http://us4.php.net/mysql_fetch_array
>
>This one in particular should help:
>********************************
>Ben
>06-Apr-2004 09:59
>One of the most common mistakes that people make with this function,
>when using it multiple times in one script, is that they forget to use
>the mysql_data_seek() function to reset the internal data pointer.
>
>When iterating through an array of MySQL results, e.g.
>
><?php
>while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
> foreach ($line as $col_value) {
> echo $col_value . '
';
> }
>}
>?>
>
>the internal data pointer for the array is advanced, incrementally,
>until there are no more elements left in the array. So, basically, if
>you copy/pasted the above code into a script TWICE, the second copy
>would not create any output. The reason is because the data pointer has
>been advanced to the end of the $line array and returned FALSE upon
>doing so.
>
>If, for some reason, you wanted to interate through the array a second
>time, perhaps grabbing a different piece of data from the same result
>set, you would have to make sure you call
>
><?php
>mysql_data_seek($result, 0);
>?>
>
>This function resets the pointer and you can re-iterate through the
>$line array, again!
>********************************
>
>-TG
>
>
>> -----Original Message-----
>> From: Derrick Hermanson [mailto:dhermanson [at] acton.com]
>> Sent: Tuesday, May 25, 2004 3:05 PM
>> To: Ron.Herhuth [at] tatumpartners.com; php-windows [at] lists.php.net
>> Subject: RE: [PHP-WIN] MS SQL Query question
>>
>>
>> try something like this
>>
>> $result = mssql_query ($query);
>> if ($result) {
>> while ($template_args = msssql_fetch_array ($res)) {
>> //what ever you want to do with the data
>> }
>> }
>>
>> Derrick Hermanson
>> Programmer/Analyst I
>> Acton International LTD.
>> 402.470.5816
>> mailto:dhermanson [at] acton.com
>>
>>
>> -----Original Message-----
>> From: Ron.Herhuth [at] tatumpartners.com
>> [mailto:Ron.Herhuth [at] tatumpartners.com]
>> Sent: Tuesday, May 25, 2004 1:30 PM
>> To: php-windows [at] lists.php.net
>> Subject: [PHP-WIN] MS SQL Query question
>>
>>
>>
>> When I run a query the syntax looks like this:
>>
>> $result = mssql_query($query);
>> $numRows = mssql_num_rows($result);
>>
>> for($i=0; $i<$numRows; $i++)
>> {
>> $row = mssql_fetch_array($result);
>> }
>>
>> but if I go to try and loop through the resultset again it wont work
>> unless I run this line again "$result = mssql_query($query);"
>>
>> I'm sure this is wasting resources but I can't seem to get
>> around it. Any
>> suggestions?
>>
>> Thanks,
>> Ron
>>
>> --
>> PHP Windows Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
>
>
------=_NextPart_000_22255_01C4426B.E19AF7E0--
AW: MS SQL Query question
IN MY (not MS) SQL I would try
mysql_data_seek ($result, 0);
to move the internal pointer to the first data row
I dont know at the moment if there is a similar function in the msxxxx =
area.
maybe it helps
robert
> -----Ursprüngliche Nachricht-----
> Von: Ron.Herhuth [at] tatumpartners.com
> [mailto:Ron.Herhuth [at] tatumpartners.com]
> Gesendet: Dienstag, 25. Mai 2004 20:30
> An: php-windows [at] lists.php.net
> Betreff: [PHP-WIN] MS SQL Query question
>
>
>
> When I run a query the syntax looks like this:
>
> $result =3D mssql_query($query);
> $numRows =3D mssql_num_rows($result);
>
> for($i=3D0; $i<$numRows; $i++)
> {
> $row =3D mssql_fetch_array($result);
> }
>
> but if I go to try and loop through the resultset again it
> wont work unless I run this line again "$result =3D
> mssql_query($query);"
>
> I'm sure this is wasting resources but I can't seem to get
> around it. Any suggestions?
>
> Thanks,
> Ron
>
>
>
>
>
>
--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: AW: MS SQL Query question
http://nl3.php.net/manual/en/function.mssql-data-seek.php
On Tue, 2004-05-25 at 12:52, Dr. Robert Probst wrote:
> IN MY (not MS) SQL I would try
>
> mysql_data_seek ($result, 0);
>
> to move the internal pointer to the first data row
> I dont know at the moment if there is a similar function in the msxxxx area.
>
> maybe it helps
>
> robert
>
>
> > -----Ursprüngliche Nachricht-----
> > Von: Ron.Herhuth [at] tatumpartners.com
> > [mailto:Ron.Herhuth [at] tatumpartners.com]
> > Gesendet: Dienstag, 25. Mai 2004 20:30
> > An: php-windows [at] lists.php.net
> > Betreff: [PHP-WIN] MS SQL Query question
> >
> >
> >
> > When I run a query the syntax looks like this:
> >
> > $result = mssql_query($query);
> > $numRows = mssql_num_rows($result);
> >
> > for($i=0; $i<$numRows; $i++)
> > {
> > $row = mssql_fetch_array($result);
> > }
> >
> > but if I go to try and loop through the resultset again it
> > wont work unless I run this line again "$result =
> > mssql_query($query);"
> >
> > I'm sure this is wasting resources but I can't seem to get
> > around it. Any suggestions?
> >
> > Thanks,
> > Ron
> >
> >
> >
> >
> >
> >
--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: MS SQL Query question
Dr. Robert Probst wrote:
> IN MY (not MS) SQL I would try
>
> mysql_data_seek ($result, 0);
>
> to move the internal pointer to the first data row
> I dont know at the moment if there is a similar function in the msxxxx area.
>
> maybe it helps
>
For MS-SQL, you mean mssql_data_seek($result, 0) ;)
http://es2.php.net/manual/en/function.mssql-data-seek.php
Regards,
Jordi.
--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php