php_d
php_d

Reputation: 115

Output from a PDO statement

Can anyone spot where I might be going wrong with the following code?

<?php

    //MySQL Database Connect
    require 'config.php';
    $unitFrom = "kilogram";
    $unitTo = "gram";
    $units = "9000";

    try{
    require 'config.php';
    $stmt = $dbh->prepare('CALL sp_get_conversion(:in_unit_from, :in_unit_to, :in_amount, @out_amount)');
    $stmt->bindParam(':in_unit_from',$unitFrom,PDO::PARAM_STR,4000);
    $stmt->bindParam(':in_unit_to',$unitTo,PDO::PARAM_STR,4000);
    $stmt->bindParam(':in_amount',$units,PDO::PARAM_STR,4000);
    $stmt->execute();
    }
    catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();

    $conversion = $dbh->query( "SELECT @out_amount" )->fetchColumn(); 
    echo $conversion;

    }   
?>

When I run the stored procedure in phpmyadmin it works fine but nothing is echoed out when I try the code above.

Thanks

Upvotes: 0

Views: 91

Answers (2)

sikander
sikander

Reputation: 2286

The following should be in the try block:

$conversion = $dbh->query( "SELECT @out_amount" )->fetchColumn(); 
echo $conversion;

You currently have it in the catch block so it will get executed only if there is an exception is generated.

Upvotes: 2

Josh
Josh

Reputation: 12566

Try handling your error as dictated here. It's how I've always worked with PDO issues.

Upvotes: 1

Related Questions