sKunKbad Posted October 30, 2007 Share Posted October 30, 2007 I need the PDO equivalent of mysql_num_rows. I thought this would work but it doesn't: $numrows = $pdo->query("SELECT * FROM tipsntricks")->fetchAll(); echo "There were " . count($numrows) . " rows in the table"; anybody know? Quote Link to comment https://forums.phpfreaks.com/topic/75318-solved-pdo-version-of-mysql_num_rows/ Share on other sites More sharing options...
sKunKbad Posted October 30, 2007 Author Share Posted October 30, 2007 I may not have been specific, but I'd like to know the number of rows in the query result, not the entire table. My example query would return the entire table, and that's not what I'm after. If for instance my query was to return 3 rows, I'd like a function(method) that returns 3, like mysql_num_rows. Quote Link to comment https://forums.phpfreaks.com/topic/75318-solved-pdo-version-of-mysql_num_rows/#findComment-381426 Share on other sites More sharing options...
GingerRobot Posted October 30, 2007 Share Posted October 30, 2007 Well, i've never work with PDO, but i would imagine you have to do it as part of the query: SELECT COUNT(*) FROM tipsntricks Edit: Indeed, certainly looks like the way: http://uk2.php.net/manual/en/function.PDOStatement-rowCount.php Take a look at the second example. Quote Link to comment https://forums.phpfreaks.com/topic/75318-solved-pdo-version-of-mysql_num_rows/#findComment-381433 Share on other sites More sharing options...
sKunKbad Posted October 30, 2007 Author Share Posted October 30, 2007 rowCount doesn't work for SELECT statements Quote Link to comment https://forums.phpfreaks.com/topic/75318-solved-pdo-version-of-mysql_num_rows/#findComment-381437 Share on other sites More sharing options...
GingerRobot Posted October 30, 2007 Share Posted October 30, 2007 Did you read the second example? Evidently not. Quote Link to comment https://forums.phpfreaks.com/topic/75318-solved-pdo-version-of-mysql_num_rows/#findComment-381442 Share on other sites More sharing options...
sKunKbad Posted October 30, 2007 Author Share Posted October 30, 2007 oh! I was looking at the second part of the first example. Thanks, I'll give it a shot. Quote Link to comment https://forums.phpfreaks.com/topic/75318-solved-pdo-version-of-mysql_num_rows/#findComment-381450 Share on other sites More sharing options...
sKunKbad Posted October 30, 2007 Author Share Posted October 30, 2007 I still can't get it to do what I want to do. Here is my standard connection / query / echo script: <?php try{ $pdo = new PDO('mysql:host=p13mysql65.secureserver.net;dbname=tips_table', 'tips_table', 'ChromeDomeX'); foreach ($pdo->query('SELECT * FROM tipsntricks WHERE tipType = "compatibility"') as $row) { echo "<p>Tip number " . $row['tipNum'] . " is:<br />"; echo $row['tipTitle'] . "</p>"; } $pdo2 = $pdo->query('SELECT * FROM tipsntricks WHERE tipType = "performance"'); $result = $pdo2->fetch(PDO::FETCH_ASSOC); extract($result); echo "<p>Tip number " . $tipNum . " is:<br />"; echo $tipTitle . "</p>"; $pdo = null; } catch(PDOException $e){ echo $e->getMessage(); } ?> and for instance, I need to be able to check to see how many tips are in the performance catagory, and I need the script to return a number, like how mysql_num_rows works. If there are 4 performance tips, I need the script to return 4. I tried the script in example 2 on the rowCount page, but it's not working. Quote Link to comment https://forums.phpfreaks.com/topic/75318-solved-pdo-version-of-mysql_num_rows/#findComment-381470 Share on other sites More sharing options...
sKunKbad Posted October 30, 2007 Author Share Posted October 30, 2007 Ok, this works: $pdo = new PDO('mysql:host=p6mysql1.whatever.net;dbname=tips', 'tips, 'ChromeDomeX'); $sql = $pdo->prepare('SELECT * FROM tipsntricks WHERE tipType = "performance"'); $sql->execute(); echo "Number of rows: " . count($sql->fetchAll()); $pdo = null; but there can't be any SELECT queries above the prepare statement. Quote Link to comment https://forums.phpfreaks.com/topic/75318-solved-pdo-version-of-mysql_num_rows/#findComment-381548 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.