odbc(或mysql)资源如何在php中工作?

发布于 2024-08-15 16:16:53 字数 497 浏览 1 评论 0原文

当您运行这样的查询时:

$query = "SELECT * FROM table";
$result = odbc_exec($dbh, $query);
while ($row = odbc_fetch_array($result)) {
    print_r($row);
}

$result 中存储的资源是否指向运行 php 的服务器上存在的数据?或者是指向数据库中的数据?换句话说,正如 while 循环所做的那样,PHP 是在每次迭代时与数据库对话,还是从应用程序端的某个源中提取 $row ?

这对我来说很重要,因为我有一个数据库,我正在使用 ODBC 和 PHP 通过 VPN 与之通信。上周末发生了一些奇怪的事情,在 while 循环期间发生了巨大的暂停。因此,在迭代之间,脚本将停止执行几秒钟甚至几分钟。这种情况发生的地方似乎完全是随机的。我想知道每次迭代是否需要通过 VPN 与服务器通信,也许连接不稳定,或者我的 ODBC 驱动程序 (FreeTDS) 是否出了问题。

When you run a query like so:

$query = "SELECT * FROM table";
$result = odbc_exec($dbh, $query);
while ($row = odbc_fetch_array($result)) {
    print_r($row);
}

Does the resource stored in $result point to data that exists on the server running php? Or is pointing to data in the database? Put another way, as the while loop does it's thing ,is PHP talking to the DB every iteration or is it pulling that $row from some source on the application side?

Where this is mattering to me is I have a database I'm talking to over VPN using ODBC with PHP. This last weekend something strange has happened where huge pauses are happening during the while loop. So between iterations, the script will stop execution for seconds and up to minutes. It seems to be completely random where this happens. I'm wondering if I need to talk to the server over VPN each iteration and maybe the connection is flaky or if something has gone wrong with my ODBC driver (FreeTDS).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝天 2024-08-22 16:16:53

mysql_query 和 odbc_exec 都返回一个资源(引用自 php.net) “是一个特殊变量,保存对外部资源的引用。”这表明服务器每次迭代都在与数据库服务器通信,但我不确定。

然而,我们在这里讨论的是两个联系。第一个是与 PHP 服务器的连接,第二个是 PHP 服务器和数据库服务器之间的连接。如果两台服务器都有快速连接,您遇到的奇怪行为可能与您的 VPN 无关。

mysql_query and odbc_exec both return a resource which (quote from php.net) "is a special variable, holding a reference to an external resource." This suggests the server is talking with the database server every iteration, I am not sure though.

However, there are 2 connections we are talking about here. The first being your connection with the PHP server, and the second one being the connection between the PHP server and the database server. If both servers have a fast connection, the strange behaviour you are experiencing might not have anything to do with your VPN.

明明#如月 2024-08-22 16:16:53

资源标识 PHP 用于与外部资源交互的内部数据结构。

对于 mysql_query() 返回的资源,此数据结构将包括查询返回的行(并且直到所有数据都已返回或连接失败时才会返回)。然而,这种行为是 MySQL 特有的 - 不要求 DBMS 在客户端显式请求之前返回数据。

如果在您的设置中存在一些奇怪的问题导致大量延迟,那么唯一明显的解决方案是在数据库端编译查询结果,然后将它们传递给您的 PHP 代码,无论是批量的还是作为一个整体(想想 web 服务) 。

C.

The resource identifies the internal data structure used by PHP for interacting with the external resource.

In the case of the resource returned by mysql_query(), this data structure will include the rows returned by the query (and won't return until all the data has been returned or the conenction fails). However this behaviour is specific to MySQL - there is no requirement that the DBMS return the data before it is explicitly requested by the client.

If there is some strange problem causing lots of latency in your setup, then the only obvious solution would be to compile the results of the query at the database side then deliver them to your PHP code, aither batched or as a whole (think webservice).

C.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文