Re: dead loop
| From: | Meir Kriheli - MKsoft | Date: | Sun, 03 Dec 2000 11:17:45 +0000 |
| Subject: | Re: dead loop | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-4976@lists.php.net to get a copy of this message | ||
Hi Teddy,
1) I guess that there is a problem with the $max var. On some databases
(Iknow Oracle and Interbase) the num_rows function won't return
the actual rows becaues of asynchronyous result sets.
You don't need the $max variable any way, and you can lose it.
Simply changes the loop to:
for($i=0;$i<count($run_s);$i++) {
echo $ran_s[$i] . '<BR>';
}
note that the array you are using is zero based, so you should start
from 0.
2) variable names in php are case sensetive, and you have
a typo in the reset statement, it should be reset(run_s)
instead of reset(run_S).
the reset statement is redundent. You don't need it.
3) Don't store the result set of a query in an array. In case of large
result sets
you'll and up using a lot of memory. Instead, if you just want to print
the
results of the query, change your code to:
<?php
require 'functions.php';
$link = mssql_connect($DB_SERVER,$DB_LOGIN,$DB_PASSWORD);
if(!$link) {
DisplayErr("Can not connect to the DB");
exit();
}
$SEC_DB = mssql_select_db($DB,$link);
if(!$SEC_DB) {
DisplayErr("Can not select the DB");
exit();
}
$result = mssql_query("SELECT Q_ID FROM Quiz_Content WHERE Subject_ID =
2",$link);
if(!$result) {
DisplayErr("Can not execute the statement");
exit();
}
// the \n at the end of the line is for readabilidy of
// the source on the brwoser
while($row = mssql_fetch_array($result)) {
echo $row["Q_ID"] . "<BR>\n";
}
// don't forget to release the result set
mssql_free_result($result);
?>
This should use less resources, and is more efficient.
HTH
--
Meir Kriheli
MKsoft computer systems
'There's someone in my head but it's not me" - Pink Floyd
----- Original Message -----
From: "catteddy" <catteddy@netvigator.com>
To: <php-db@lists.php.net>
Sent: Sunday, December 03, 2000 12:42 AM
Subject: [PHP-DB] dead loop
Hello, I'm Teddy. I write a program file like belo using PHP
<?php
require 'functions.php';
$link = mssql_connect($DB_SERVER,$DB_LOGIN,$DB_PASSWORD);
if(!$link) {
DisplayErr("Can not connect to the DB");
exit();
}
$SEC_DB = mssql_select_db($DB,$link);
if(!$SEC_DB) {
DisplayErr("Can not select the DB");
exit();
}
$result = mssql_query("SELECT Q_ID FROM Quiz_Content WHERE Subject_ID =
2",$link);
if(!$result) {
DisplayErr("Can not execute the statement");
exit();
}
$max = mssql_num_rows($result);
echo $max;
$ran_s[] = 0;
while($row = mssql_fetch_array($result)) {
$ran_s[] = $row["Q_ID"];
}
reset($ran_S);
for($i=1;$i<=$max;$i++) {
echo $ran_s[$i];
echo "<BR>";
}
?>
I was using WindowNT4.0 as the web server and Microsoft SQL2000. They both
install in the same machine(PII 400 with 128MB RAM).
Every time I execute the program. It just use up almost all the resource in
the NT. When I see the TaskManager. PHP is using 99% resource.
I had ever try another methord to output the result. The problem may occur
in the last For loop. It just display some number very big. I had attach the
script of the table.
Please help me!
Thanks a lot
Teddy