Jump to content
Larry Ullman's Book Forums

Recommended Posts

I was trying to force an error on the following script in order to execute the catch routine by replacing the conditional statement

if ($r && $r->rowCount() > 0)  with if ($r && $r->rowCount() = 0)

and for some reason, I received the ubiquitous 500 Internal Server Error. It wasn't until I modified the conditional to read if ($r && $r->rowCount() < 0) would the catch routine be executed. Any idea why this might have happened?

 

 

try {
    
    $q = 'SELECT id, title, content, DATE_FORMAT(dateAdded, "%e %M %Y") AS dateAdded FROM pages ORDER BY dateAdded DESC LIMIT 3'; 
    $r = $pdo->query($q);
    
    // Check that rows were returned:
    if ($r && $r->rowCount() > 0) {
 
        // Set the fetch mode:
        $r->setFetchMode(PDO::FETCH_CLASS, 'Page');
 
        // Records will be fetched in the view:
        include('views/index.html');
 
    } else { // Problem!
        throw new Exception('No content is available to be viewed at this time.');
    }
        
} catch (Exception $e) { // Catch generic Exceptions.
    include('views/error.html');
}
Link to comment
Share on other sites

Another question, in script 9.11, the author assigns values to parameters to be used in a prepared statement query:

 

 

   // Check against the database:
        $q = 'SELECT id, userType, username, email FROM users WHERE email=:email AND pass=SHA1(:pass)';
        $stmt = $pdo->prepare($q);
        $r = $stmt->execute(array(':email' => $email->getValue(), ':pass' => $password->getValue()));
 
I received an error so I simply used the values of email and password and the script worked. Is the getValue() method inherent within the PDO class?
 
 
nevermind, I found the answer right in front of me.
Link to comment
Share on other sites

I would first start by confirming there isn't an error in your SQL command. I believe the row count should only be less than 0 if there's an error in executing the query, but I could be wrong on that. 

Link to comment
Share on other sites

well, the SQL query works correctly because the conditional statement executes as it's designed to do. I only wanted to modify the conditional to force the script to execute the exception part of the block just to see the results. What confused me was that when the conditional statement was modified so that the row count was equivalent to 0, I would receive an error but when I modified the statement so that the row count was less than 0, it executed the exception part of the code. It's not important. The code works fine so I'll leave it at that.

Link to comment
Share on other sites

  • 2 weeks later...
 Share

×
×
  • Create New...