如何使用 ODBC 连接在 PHP 中获取结果集中的计数或行数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4117351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:01:33  来源:igfitidea点击:

How to Get The Count or The Number Of Rows In A Result Set In PHP using ODBC Connection?

phpsqlodbccountsybase

提问by TopDeveloper

While I build a web page in My PHP web application, My Connection works ok but When I want to get the count of rows of the SELECT Statement I used in my query, It gives me -1 !! although my result set has about 10 rows.

当我在我的 PHP Web 应用程序中构建一个网页时,我的连接工作正常,但是当我想获取我在查询中使用的 SELECT 语句的行数时,它给了我 -1 !虽然我的结果集大约有 10 行。

I would like to get the actual number of result set rows. I searched the PHP Manual & documentation but I do not find a direct way like a Count function or something like that.

我想获得结果集行的实际数量。我搜索了 PHP 手册和文档,但没有找到像 Count 函数或类似的直接方法。

I wonder if I have to make a Count(*) SQL Statement in another query and attach it to my Connection to get the Count of Rows ?

我想知道是否必须在另一个查询中创建一个 Count(*) SQL 语句并将其附加到我的 Connection 以获取行数?

Does any one knows an easy and direct way to get that ?

有没有人知道一种简单而直接的方法来获得它?

the odbc_num_rows function always gives -1 in result so I can not get the actual number of rows.

odbc_num_rows 函数总是在结果中给出 -1,所以我无法获得实际的行数。

My Programming langauge is PHP and My Database Engine is Sybase and The Way to connect to Database is ODBC.

我的编程语言是 PHP,我的数据库引擎是 Sybase,连接数据库的方式是 ODBC。

Here are you the Code I used:-

这是我使用的代码:-

<?PHP

//PHP Code to connect to a certain database using ODBC and getting information from it

//Determining The Database Connection Parameters
$database = 'DatabaseName';
$username = 'UserName';
$password = 'Password';

//Opening the Connection
$conn = odbc_connect($database,$username,$password);

//Checking The Connection
if (!$conn)
{
exit("Connection Failed: " . $conn);
}

//Preparing The Query
$sql = "SELECT * FROM Table1 WHERE Field1='$v_Field1'";

//Executing The Query
$rs = odbc_exec($conn,$sql);

//Checking The Result Set
if (!$rs)
{
exit("Error in SQL");
}

echo "<p align='Center'><h1>The Results</h1></p>";

while ( odbc_fetch_row($rs) )

{
  $field1 = odbc_result($rs,1);
  $field2 = odbc_result($rs,2);
  $field3 = odbc_result($rs,3);
  echo "field1 : " . $field1 ;
  echo "field2 : " . $field2 ;
  echo "field3 : " . $field3 ;
}

$RowNumber = odbc_num_rows($rs);

echo "The Number of Selected Rows = " . $RowsNumber ; 

//Closing The Connection
odbc_close($conn);

?>

Thanks for your Help :)

感谢您的帮助 :)

回答by Pekka

odbc_num_rowsseems to be reliable for INSERT, UPDATE, and DELETE queries only.

odbc_num_rows似乎只对 INSERT、UPDATE 和 DELETE 查询可靠。

The manualsays:

手册说:

Using odbc_num_rows() to determine the number of rows available after a SELECT will return -1 with many drivers.

使用 odbc_num_rows() 确定 SELECT 后可用的行数将返回 -1 与许多驱动程序。

one way around this behaviour is to do a COUNT(*)in SQL instead. See herefor an example.

解决此行为的一种方法是COUNT(*)在 SQL 中执行 a 。有关示例,请参见此处

回答by Ramin Rezazadeh

in php.net:

在 php.net 中:

The easy way to count the rows in an odbc resultset where the driver returns -1 is to let SQL do the work:

计算驱动程序返回 -1 的 odbc 结果集中的行数的简单方法是让 SQL 来完成这项工作:

<?php

    $conn = odbc_connect("dsn", "", "");
    $rs = odbc_exec($conn, "SELECT Count(*) AS counter FROM tablename WHERE fieldname='" . $value . "'");
    $arr = odbc_fetch_array($rs);
    echo $arr['counter'];

?>

回答by PerformanceDBA

On what basis, do you expect odbc_num_rowsto return anything other than -1 ?

在什么基础上,您希望odbc_num_rows返回 -1 以外的任何内容?

We have the fact from the manuals, that OBDC does not support @@ROWCOUNT / odbc_num_rows. So there is no basis for expecting that it "should" return anything other than that which is documented, -1 in all circumstances.

我们从手册中得知,OBDC 不支持@@ROWCOUNT / odbc_num_rows. 因此,没有依据期望它“应该”返回除记录之外的任何内容,在所有情况下都为 -1。

Even if you used Sybase directly (instead of via ODBC), you would have the same "problem".

即使您直接使用 Sybase(而不是通过 ODBC),您也会遇到同样的“问题”。

  • odbc_num_rowsreturns @@ROWCOUNT, which is the rows inserted/updated/deleted by the immediately preceding command. -1 is the correct, documented value, if the immediately preceding command is notinsert/update/delete.

  • It has nothing to do with rows in a table.

  • odbc_num_rows返回@@ROWCOUNT,它是由前一个命令插入/更新/删除的行。-1 是正确的记录值,如果前一个命令不是插入/更新/删除。

  • 它与表中的行无关。

Use another batch, and either one of the documentedmethods to obtain rows in a table, and load the value into a variable:

使用另一个批处理和任一记录的方法来获取table 中的行,并将值加载到变量中:

  • SELECT @Count = COUNT(*) -- slow, requires I/O
    or
  • SELECT @Count = ROW_COUNT(db_id, object_id) -- very fast, no I/O
  • SELECT @Count = COUNT(*) -- 慢,需要 I/O
  • SELECT @Count = ROW_COUNT(db_id, object_id) -- 非常快,没有 I/O

Then interrogate the result array, to obtain the variable, not odbc_num_rows, which will continue returning -1.

然后查询结果数组,获取变量, not odbc_num_rows,它将继续返回-1。