Jump to content
Larry Ullman's Book Forums

Recommended Posts

I've recently discovered PHP PDO objects for communicating with mySQL. As is the case with progress, some

desired features appear lacking. PHP PDO's don't seem to offer the mysqli_num_rows equivalent:

 

 

// Count the number of returned rows:
$num = mysqli_num_rows($r);

if ($num > 0) { // If it ran OK, display the records.

// Print how many users there are:
echo "<p>There are currently $num registered users.</p>\n";...................

 

would someone enlighten on achieving the same results using PDO syntax, or is using the mixed syntax

permissible before I begin using PDO's for all the queries?

 

 

View Users: Partial Revision

 

<?php


$page_title = 'View the Current Users';
include ('./inc/header.php');

// Page header:
echo '<h1>Registered Users</h1>';

require_once ('../pdo_connect.php'); // Connect to the db.

// Make the query:

//Will replace with prepared statement

$sql = "SELECT CONCAT(last_name, ', ', first_name) AS name, DATE_FORMAT(registration_date, '%M %d, %Y') AS dr FROM users ORDER BY registration_date ASC";	

// Table header.
echo <<<EOT
   	<table class="app_table" summary="The Current roster of users registered in our application.">
    <caption>The Current Registered Users:<span id="summaryView"></span></caption>
	<thead>
	<tr>

	<th class="Corner">Name</th>
	<th>Date Registred</th>
	</tr>
	</thead>
    <tbody>
EOT;

try {
foreach($DBH->query($sql) as $row){


	echo '<tr><td align="left">' . $row['name'] . '</td><td align="left">' . $row['dr'] . '</td></tr>';

	}

   echo '</tbody></table>'; // Close the table.

	// $DBH = null;//using a persistent connect see php.net
} catch (PDOException $e){

echo'<p class="error">' . "Error!: " . $e->getMessage() . '</p>';
die();
}


include ('./inc/footer.php');
?>

Link to comment
Share on other sites

Just use the method rowCount() on your PDO statement object. Don't mix anything, you can do just about everything with PDO except multiple queries I believe, and some advanced functionality that you can use with mysqli.

 

Here is some sample code from my PDO queries. It's for a poll. If you're interested in PDO, you should buy Murach's PHP and MySQL book by Joel Murach and Ray Harris. There is some great material on PDO and how it compares to mysqli and mysql.

 

   //define and run the query
       try {
       $success = '';
       $row_count = '';
       $query= "INSERT INTO mailing_list_subscribers(first_name, last_name, email)
       VALUES (:first_name, :last_name, :email)";
       $statement=$db->prepare($query);
       $statement->bindValue(':first_name', $first_name);
       $statement->bindValue(':last_name', $last_name);
       $statement->bindValue(':email', $email);
       $success = $statement->execute();
       $row_count = $statement->rowCount();
       $statement->closeCursor();
       } catch (PDOException $e) {
           $error_message = $e->getMessage();
           if (!$live){
               echo '<p class="error">' . $error_message . '</p>';
           } else {
               echo '<p class="error">You were not added to our mailing list... You may have previously subscribed</p>';
           }
       }
       //print a message
       if ($row_count == 1){
         echo '<p class="success">You were added to our mailing list!</p>';
         $first_name = $last_name = $email = '';
       } else {
         echo '<p class="error">You were not added to our mailing list... You may have previously subscribed</p>';
       }

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...