search
HomeBackend DevelopmentPHP Tutorial请问pdo如何计算结果集的数目呢?

有这样一段代码,怎么取也取不到结果集的数目....请各位大神看看...
$stmt=$dbc->prepare('select count(*) from loginlog');
    $rows=$stmt->execute();
    pageDivide($rows,10);
$result=$dbc->prepare('select * from loginlog order by logintime desc limit $sqlfirst,$shownu');
$result->execute();
echo'一共有'.$rows.'条登录记录';
echo '

';
echo '
';
echo '
';
echo '';
echo '';
echo '';
echo '';
echo '';
echo '';
echo '';
echo '';
echo '';
echo '';
while($row=$result->fetch()){
echo '';
echo '';
echo '';
echo '';
echo '';
echo '';
echo '';
echo '';
echo '';
}
echo '
用户名密码登录IP登录时间登录状态尝试次数在线状态
'.$row['name'].''.$row['password'].''.$row['ip'].''.$row['logintime'].''.$row['status'].''.'1'.''.'在线'.'
';
echo '';
echo '
';
echo '
';
echo '
';

回复讨论(解决方案)

$result = $stmt->fetch(PDO::FETCH_NUM);
echo $result[0] ; //这个才是

$result = $stmt->fetch(PDO::FETCH_NUM);
echo $result[0] ; //这个才是
请问那是这样写吗?
$stmt=$dbc->prepare('select * from loginlog');
    $rows=$stmt->execute();
$rowsNum=$rows->fetch(PDO::FETCH_NUM);

LZ应该是$stmt=$dbc->prepare('select * from loginlog');
    $rows=$stmt->execute();
$rowsNum=$rows->fetch(PDO::FETCH_ASSOC);
print_r($rowsNum->rowCount());

LZ应该是$stmt=$dbc->prepare('select * from loginlog');
    $rows=$stmt->query();
$rowsNum=$rows->fetch(PDO::FETCH_ASSOC);
print_r($rowsNum->rowCount());
上面的有点小问题,是print_r($rows->rowCount());


LZ应该是$stmt=$dbc->prepare('select * from loginlog');
    $rows=$stmt->query();
$rowsNum=$rows->fetch(PDO::FETCH_ASSOC);
print_r($rowsNum->rowCount());
上面的有点小问题,是print_r($rows->rowCount());
Fatal error: Call to a member function fetch() on a non-object i
$stmt=$dbc->prepare('select * from loginlog');
    $rows=$stmt->execute();
$rowsNum=$rows->fetch(PDO::FETCH_ASSOC);
$rowsN=$rows->rowCount();

$stmt=$dbc->prepare('select * from loginlog');
    $rows= $stmt->execute();
$rowsNum= $stmt->fetch(PDO::FETCH_NUM); 

$stmt=$dbc->prepare('select * from loginlog');
    $rows= $stmt->execute();
$rowsNum= $stmt->fetch(PDO::FETCH_NUM); 
echo 一个array。。。要rowCount();吗?

$stmt=$dbc->prepare('select count(*) from loginlog');
$stmt->execute();
$rowsNum=$stmt->fetch(PDO::FETCH_NUM);  //返回一个数组
print_r($rowsNum);

或者这样:
$stmt=$dbc->prepare('select count(*) from loginlog');
$stmt->execute();
$rowsNum = $stmt->fetchColumn();
echo $rowsNum;

int PDOStatement::rowCount ( void )

PDOStatement::rowCount() 返回上一个由对应的 PDOStatement 对象执行DELETE、 INSERT、或 UPDATE 语句受影响的行数。

如果上一条由相关 PDOStatement 执行的 SQL 语句是一条 SELECT 语句,有些数据可能返回由此语句返回的行数。但这种方式不能保证对所有数据有效,且对于可移植的应用不应依赖于此方式。 

$stmt=$dbc->prepare('select count(*) from loginlog');
$stmt->execute();
$rowsNum=$stmt->fetch(PDO::FETCH_NUM);  //返回一个数组
print_r($rowsNum);

或者这样:
$stmt=$dbc->prepare('select count(*) from loginlog');
$stmt->execute();
$rowsNum = $stmt->fetchColumn();
echo $rowsNum;

$stmt=$dbc->prepare('select count(*) from loginlog');
    $rows=$stmt->execute();
    $rowsNum = $rows->fetchColumn();
    pageDivide($rowsNum,10);
$result=$dbc->prepare('select * from loginlog order by logintime desc limit $sqlfirst,$shownu');
$result->execute();
//if($stmt){
/*$result=mysql_query('select * from loginlog order by logintime desc ');
$total=mysql_num_rows($result);
pageDivide($total,10);
$result=mysql_query("select * from loginlog order by logintime desc limit $sqlfirst,$shownu ");*/
echo'一共有'.$rowsNum.'条登录记录';
echo '

';
echo '
';
echo '
';
echo '';
echo '';
echo '';
echo '';
echo '';
echo '';
echo '';
echo '';
echo '';
echo '';
//while($tota=mysql_fetch_assoc($result)){
while($row=$result->fetch()){
echo '';
echo '';
echo '';
echo '';
echo '';
echo '';
echo '';
echo '';
echo '';
}
echo '
用户名密码登录IP登录时间登录状态尝试次数在线状态
'.$row['name'].''.$row['password'].''.$row['ip'].''.$row['logintime'].''.$row['status'].''.'1'.''.'在线'.'
';
echo '';
echo '
';
echo '
';
echo '
';

代码整体是这样的,但是页面老是报错
Fatal error: Call to a member function fetchColumn() on a non-object 这个是为什么呢?

 $rows= $stmt->execute();
    $rowsNum =  $stmt->fetchColumn();

 $rows= $stmt->execute();
    $rowsNum =  $stmt->fetchColumn();
$stmt=$dbc->prepare('select count(*) from loginlog');
    $rows=$stmt->execute();
    $rowsNum = $rows->fetchColumn();
是这样写的呀...报错~

本帖最后由 xuzuning 于 2013-06-26 15:38:19 编辑

$stmt->execute();
$rowsNum =  $stmt->fetchColumn();

$stmt->execute();
$rowsNum =  $stmt->fetchColumn();
嗯嗯 这样是对了 谢谢你,可以和我说下是为什么吗?我好避免以后犯这样的错误

PDO::prepare 返回一个 PDOStatement 对象,就是你的那个 $stmt
而 PDOStatement::execute 返回的是一个逻辑值,表示执行成功与否
如果你写作 $rows=$stmt->execute();
那么 $rows 只是一个 true
当然也就没有 fetchColumn 方法了,于是就报错了

一是要看手册,不但要看用法,而且要学会看原型声明
二是要学会看错误信息

PDO::prepare 返回一个 PDOStatement 对象,就是你的那个 $stmt
而 PDOStatement::execute 返回的是一个逻辑值,表示执行成功与否
如果你写作 $rows=$stmt->execute();
那么 $rows 只是一个 true
当然也就没有 fetchColumn 方法了,于是就报错了

一是要看手册,不但要看用法,而且要学会看原型声明
二是要学会看错误信息

嗯嗯  懂了,谢谢你...我一直是项目驱动方式学习php的,遇到问题才去翻手册...

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What is the difference between absolute and idle session timeouts?What is the difference between absolute and idle session timeouts?May 03, 2025 am 12:21 AM

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

What steps would you take if sessions aren't working on your server?What steps would you take if sessions aren't working on your server?May 03, 2025 am 12:19 AM

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

What is the significance of the session_start() function?What is the significance of the session_start() function?May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

What is the importance of setting the httponly flag for session cookies?What is the importance of setting the httponly flag for session cookies?May 03, 2025 am 12:10 AM

Setting the httponly flag is crucial for session cookies because it can effectively prevent XSS attacks and protect user session information. Specifically, 1) the httponly flag prevents JavaScript from accessing cookies, 2) the flag can be set through setcookies and make_response in PHP and Flask, 3) Although it cannot be prevented from all attacks, it should be part of the overall security policy.

What problem do PHP sessions solve in web development?What problem do PHP sessions solve in web development?May 03, 2025 am 12:02 AM

PHPsessionssolvetheproblemofmaintainingstateacrossmultipleHTTPrequestsbystoringdataontheserverandassociatingitwithauniquesessionID.1)Theystoredataserver-side,typicallyinfilesordatabases,anduseasessionIDstoredinacookietoretrievedata.2)Sessionsenhances

What data can be stored in a PHP session?What data can be stored in a PHP session?May 02, 2025 am 12:17 AM

PHPsessionscanstorestrings,numbers,arrays,andobjects.1.Strings:textdatalikeusernames.2.Numbers:integersorfloatsforcounters.3.Arrays:listslikeshoppingcarts.4.Objects:complexstructuresthatareserialized.

How do you start a PHP session?How do you start a PHP session?May 02, 2025 am 12:16 AM

TostartaPHPsession,usesession_start()atthescript'sbeginning.1)Placeitbeforeanyoutputtosetthesessioncookie.2)Usesessionsforuserdatalikeloginstatusorshoppingcarts.3)RegeneratesessionIDstopreventfixationattacks.4)Considerusingadatabaseforsessionstoragei

What is session regeneration, and how does it improve security?What is session regeneration, and how does it improve security?May 02, 2025 am 12:15 AM

Session regeneration refers to generating a new session ID and invalidating the old ID when the user performs sensitive operations in case of session fixed attacks. The implementation steps include: 1. Detect sensitive operations, 2. Generate new session ID, 3. Destroy old session ID, 4. Update user-side session information.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor