如何在PHP中使用ODBC连接获取计数或结果集中的行数? [英] How to Get The Count or The Number Of Rows In A Result Set In PHP using ODBC Connection?

查看:1077
本文介绍了如何在PHP中使用ODBC连接获取计数或结果集中的行数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



我想知道我是否需要在另一个查询中创建一个Count并附加到我的连接以获得行的计数?



有没有人知道一个简单和直接的方法来获得?



odbc_num_rows函数总是在结果中给出-1,因此我无法获取实际的行数。



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



这里是你使用的代码: -

 <?PHP 

//使用ODBC连接到某个数据库并从中获取信息的PHP代码

//确定数据库连接参数
$ database ='DatabaseName';
$ username ='UserName';
$ password ='Password';

//打开连接
$ conn = odbc_connect($ database,$ username,$ password);

//检查连接
if(!$ conn)
{
exit(Connection Failed:。
}

//正在准备查询
$ sql =SELECT * FROM Table1 WHERE Field1 ='$ v_Field1';

//执行查询
$ rs = odbc_exec($ conn,$ sql);

//检查结果集
if(!$ rs)
{
exit(SQL中的错误);
}

echo< p align ='Center'>< h1>结果< / h1>< / p&

while(odbc_fetch_row($ rs))

{
$ field1 = odbc_result($ rs,1);
$ field2 = odbc_result($ rs,2);
$ field3 = odbc_result($ rs,3);
echofield1:。 $ field1;
echofield2:。 $ field2;
echofield3:。 $ field3;
}

$ RowNumber = odbc_num_rows($ rs);

echo所选行数=。 $ RowsNumber;

//关闭连接
odbc_close($ conn);

?>感谢您的帮助:)


$ b < >解决方案

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



手册说:


使用odbc_num_rows()来确定SELECT之后可用的行数,并返回带有许多驱动程序的-1。


行为是在SQL中做一个 COUNT(*)。有关示例,请参见此处


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.

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.

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 ?

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

My Programming langauge is PHP and My Database Engine is Sybase and The Way to connect to Database is 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 :)

解决方案

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

The manual says:

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

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

这篇关于如何在PHP中使用ODBC连接获取计数或结果集中的行数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆