Jump to content

[SOLVED] PDO version of mysql_num_rows(); ?


sKunKbad

Recommended Posts

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.

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.

 

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.