Jump to content

Slow Php Connecting To Access.


Jiraiya-Sama

Recommended Posts

Hello Hi, :)I have a question about PHP connecting to Access via ODBC.Basically, I follow the instruction listed here, the code is exactly the same.http://www.w3schools.com/PHP/php_db_odbc.aspAnyway, when i run the code, the browser just keep loading loading loading...zzz and eventually time out after 30second.the database in Access is not that big, around 2000 records in a table only, but still it just keeps loading until error.However, if i use these code to specific number of records to loop, the page loads without problems.

$num = 2000; //number of recordfor ($i=0; $i<$num; $i++){		odbc_fetch_row($rs, $i);			$compname=odbc_result($rs,"CompanyName");				$conname=odbc_result($rs,"ContactName");				echo "<tr><td>$compname</td>";				echo "<td>$conname</td></tr>"}

Any ideas or suggestions will be highly appreciated.Thanks a lot.

Link to comment
Share on other sites

It sounds like you have an infinite loop. You're using the code exactly like on the page you linked to, you haven't changed it at all?Look through the other ODBC functions, you can use odbc_num_rows to see how many rows were returned, odbc_errormsg to check for errors, etc. Use some of those functions to test the result after you run the query and before the loop.http://www.php.net/manual/en/function.odbc-num-rows.phphttp://www.php.net/manual/en/function.odbc-result-all.php

Link to comment
Share on other sites

Hi justsomeguy,thanks for your quick reply.i try odbc_errormsg, and there is no error code or error message about the connection, but when i use odbc_num_rows to check number of rows, and it shows that it is "-1" ..... i guess this is the reason why it has infinity loop.... and may i ask if you have any ideas why it is and how this can be solved?thanks again for your help.

Link to comment
Share on other sites

Hmm..

Returns the number of rows in an ODBC result. This function will return -1 on error.
Sounds like there's an error then. Of course, there's also this:
Note: Using odbc_num_rows() to determine the number of rows available after a SELECT will return -1 with many drivers.
So maybe there's not an error, maybe the Access driver just doesn't count the number of rows.Are you seeing anything get printed? Are you seeing the table headers or anything, or is the page just blank?
Link to comment
Share on other sites

I haven't done a lot of work with ODBC in PHP, but it might be better to use a for loop like you had instead of the while loop. I'm not sure why there would be a problem with the example code. Here is a function for counting the number of rows:

function best_odbc_num_rows($r)  {  ob_start();  (int)$number=odbc_result_all($r);   ob_end_clean();  return $number;}

So this is the example code, except with a for loop and that function instead of the while loop:

<html><body><?php$conn=odbc_connect('northwind','','');if (!$conn)  {exit("Connection Failed: " . $conn);}$sql="SELECT * FROM customers";$rs=odbc_exec($conn,$sql);if (!$rs)  {exit("Error in SQL");}echo "<table><tr>";echo "<th>Companyname</th>";echo "<th>Contactname</th></tr>";$num = best_odbc_num_rows($rs);for ($i=0; $i<$num; $i++){  odbc_fetch_row($rs, $i);	  $compname=odbc_result($rs,"CompanyName");  $conname=odbc_result($rs,"ContactName");  echo "<tr><td>$compname</td>";  echo "<td>$conname</td></tr>"}odbc_close($conn);echo "</table>";function best_odbc_num_rows($r)  {  ob_start();  (int)$number=odbc_result_all($r);   ob_end_clean();  return $number;}?></body></html>

Link to comment
Share on other sites

That's very nice of you. Thanks a lot.But it still has the same problems. It takes more than 30 seconds to load the page.funny thing, i try to do the same thing on other table on the same database using while (odbc_fetch_row($rs)) and it works without problems, result is displayed instantly and number of records is around 15,000 which is much larger than the original one which is only 2000 records....so so weird... i wonder if it is because of table...

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...