当我在我的 PHP Web 应用程序中构建一个网页时,我的连接工作正常,但是当我想获取我在查询中使用的 SELECT 语句的行数时,它给了我 -1 !虽然我的结果集大约有 10 行.
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 手册 &文档,但我没有找到像 Count 函数之类的直接方法.
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.
我想知道我是否必须在另一个查询中创建一个 Count(*) SQL 语句并将其附加到我的连接以获取行数?
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 ?
有人知道一种简单直接的方法吗?
Does any one knows an easy and direct way to get that ?
odbc_num_rows 函数总是在结果中给出 -1,所以我无法获得实际的行数.
the odbc_num_rows function always gives -1 in result so I can not get the actual number of rows.
我的编程语言是 PHP,我的数据库引擎是 Sybase,连接数据库的方式是 ODBC.
My Programming langauge is PHP and My Database Engine is Sybase and The Way to connect to Database is ODBC.
这是我使用的代码:-
<?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);
?>
感谢您的帮助:)
odbc_num_rows
似乎仅适用于 INSERT、UPDATE 和 DELETE 查询.
odbc_num_rows
seems to be reliable for INSERT, UPDATE, and DELETE queries only.
手册说:
使用 odbc_num_rows() 来确定 SELECT 后可用的行数将返回 -1 与许多驱动程序.
Using odbc_num_rows() to determine the number of rows available after a SELECT will return -1 with many drivers.
解决此行为的一种方法是在 SQL 中执行 COUNT(*)
.请参阅此处以获取示例.
one way around this behaviour is to do a COUNT(*)
in SQL instead. See here for an example.
这篇关于如何使用 ODBC 连接在 PHP 中获取结果集中的计数或行数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!