Jump to content

Unable to pass PDOStatement object to a function


jaymuntz

Recommended Posts

Sample code is below.  I am unable to pass a PDOStatement object to a function.  It appears that the function never gets called.  You can see below that my function just throws an Exception, but the Exception never gets thrown if I've attempted to pass in the PDOStatement object.  Running this script in the browser, I get a blank screen with no warnings or errors.

 

I'm baffled.  I'm trying to copy a design pattern from page 301 of this book:

PHP Objects Patterns , and Practices

 

My Code:

[pre]

<?php

error_reporting(E_ALL);

 

$PDO = new PDO( "mysql:host=localhost;dbname=tewth", "root", "" );

$PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

 

$selectByParentId = $PDO->prepare("SELECT name, parentId, sortOrder, id FROM tbl_genres WHERE parentId=:parentId");

           

$vals = array(

":parentId"=>1

);

           

//EXECUTE ONE OR THE OTHER

executeTheQuery1($selectByParentId, $vals);  //won't throw the exception

//executeTheQuery2(null, $vals);   //will throw the exception

 

//THIS WORKS CORRECTLY IF UNCOMMENTED

//$selectByParentId->execute( $vals );

//$array = $selectByParentId->fetch( );

//print_r($array);

 

function executeTheQuery1(PDOStatement $stmt, $values)

{

throw new Exception("1 - If we could get here, we'd execute the query.");

}

         

 

function executeTheQuery2(PDOStatement $stmt=null, $values)

{

throw new Exception("2 - If we could get here, we'd execute the query.");

}

 

?>

 

[/pre]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.