PHP 'db2_prepare failed' error

The place for general PHP questions and hints for PHP on IBM i
Post Reply
john2770
Posts: 7
Joined: Wed Oct 07, 2015 5:10 pm

PHP 'db2_prepare failed' error

Post by john2770 » Wed Oct 07, 2015 5:14 pm

I'm using PHP with IBMi that uses db2. I have store procedure that in a testProcedure.php code is below:

Code: Select all

    <?php 
    				
    				$inCuCode   = "GLLL01";
    				$inProdCode = "71MRT18818WX";
    				$inBuyNum   = "BUNZLS";
    				
    				// Prepare Stored Procedure call //
    				$proc = 'CALL WLKLIB.DSH3518SQ(?,?,?)';
    				$stmt = db2_prepare($conn, $proc) or die("db2_prepare failed         " . db2_stmt_error(). " and " .db2_stmt_errormsg());
    				db2_bind_param($stmt, 1, 'inCuCode', DB2_PARAM_IN,DB2_CHAR);
    				db2_bind_param($stmt, 2, 'inProdCode', DB2_PARAM_IN,DB2_CHAR);
    				db2_bind_param($stmt, 3, 'inBuyNum', DB2_PARAM_IN,DB2_CHAR);
    				db2_execute($stmt);
    				while (db2_fetch_row($stmt)) {
    				    $Fld1  = db2_result($stmt, 0);
    				    $Fld2 =  db2_result($stmt, 1);
    				    $Fld3 =  db2_result($stmt, 2);
    				    $Fld4 =  db2_result($stmt, 3);
    				    $Fld5 =  db2_result($stmt, 4);
    				    $Fld6 =  db2_result($stmt, 5);
    				    $Fld7 =  db2_result($stmt, 6);
    				    $Fld8 =  db2_result($stmt, 7);
    				    $Fld9 =  db2_result($stmt, 8);
    				    $Fld10 =  db2_result($stmt, 9);
    				    $Fld11 =  db2_result($stmt, 10);
    				    $Fld12 =  db2_result($stmt, 11);
    				    $Fld13 =  db2_result($stmt, 12);
    				    ?>
    				            <p><?php echo $Fld1;?> </p>
    				    	    <p><?php echo $Fld2;?> </p>
    				    		<p><?php echo $Fld3;?> </p>
    				            <p><?php echo $Fld4;?> </p>
    				    		<p><?php echo $Fld5;?> </p>
    				    		<p><?php echo $Fld6;?> </p>
    				    		<p><?php echo $Fld7;?> </p>
    				    		<p><?php echo $Fld8;?> </p>
    				    		<p><?php echo $Fld9;?> </p>
    				    		<p><?php echo $Fld10;?> </p>
    				    		<p><?php echo $Fld11;?> </p>
    				    		<p><?php echo $Fld12;?> </p>
    				    		<p><?php echo $Fld13;?> </p>
    				      <?php }?> 
When this file is execute it runs fine and displays the data, but when it is incorporated with my other code give an error saying "db2_prepare failed" and its the same code i'm using both places can you please me in solve this. p.s the database connect in reference at the top.

scottgcampbell
Posts: 187
Joined: Wed Apr 22, 2009 2:29 pm
Location: Edmonton, AB, Canada

Re: PHP 'db2_prepare failed' error

Post by scottgcampbell » Wed Oct 07, 2015 6:26 pm

What is the error code and message?

Is it the same environment where the code is running (same server)?

Is it the same user connecting in both instances, since you said the database connect was in a different location (maybe a permissions issue)?

Scott

john2770
Posts: 7
Joined: Wed Oct 07, 2015 5:10 pm

Re: PHP 'db2_prepare failed' error

Post by john2770 » Wed Oct 07, 2015 7:18 pm

This is all it displays "error code and message?"
Yes it is on the same server.
Yes it is the same user connecting, this is the connection script

Code: Select all

$conn = db2_connect("*LOCAL", '', ''); 
if ($conn) {
    echo "<!- Connection succeeded. -><br>\n";
}
else {
    echo "<!- Connection failed. -><br>\n";
}

scottgcampbell
Posts: 187
Joined: Wed Apr 22, 2009 2:29 pm
Location: Edmonton, AB, Canada

Re: PHP 'db2_prepare failed' error

Post by scottgcampbell » Wed Oct 07, 2015 8:17 pm

What is the output from this line:

Code: Select all

$stmt = db2_prepare($conn, $proc) or die("db2_prepare failed         " . db2_stmt_error(). " and " .db2_stmt_errormsg());
When it fails? Is there no error number or message?
Are you sure it is failing on that line? If you are adding it into other code it is possible it is failing elsewhere. Maybe change the message on that line and ensure the resulting error message produced matches.

You could also change the code a little to something like:

Code: Select all

$stmt  = db2_prepare ( $conn, $proc);
if (! $stmt ){
    // debug info
    var_dump($stmt);
    echo ( db2_stmt_error () . " - Query: Prepare error:" . db2_stmt_errormsg () );
    exit;
}
so you can do a little more before dying.

Post Reply