Search code examples
phpsql-serverpdoprocedure

PHP PDO Procedure


I couldn't find the error in this Code. Can anyone help?

$stmt = $DB->prepare("CALL dbo.HisIsTheProcedureName(?,?,?)");
$stmt->bindParam( 1, $_POST['Val01'], PDO::PARAM_STR,8000);
$stmt->bindParam( 2, $_POST['Val02'], PDO::PARAM_STR,8000);
$stmt->bindParam( 3, $_POST['Val03'], PDO::PARAM_STR,8000);
$stmt->execute();

After using debugDumpParams(), the following Dump is shown:

SQL: [30] CALL dbo.InsertFeedback(?,?,?)
Params:  3
Key: Position #0:
paramno=0
name=[0] ""
is_param=1
param_type=2
Key: Position #1:
paramno=1
name=[0] ""
is_param=1
param_type=2
Key: Position #2:
paramno=2
name=[0] ""
is_param=1
param_type=2

I don't get any Syntax or PHP errors.


Solution

  • You don't need to use PDOStatement::bindParam() except in very special circumstances. Just pass the parameters to PDOStatement::execute() instead. As for your question, it's possible you're missing some POST values. Ensure you're passing valid strings to the query, and check your return values.

    $val01 = $_POST["Val01"] ?? "";
    $val02 = $_POST["Val02"] ?? "";
    $val03 = $_POST["Val03"] ?? "";
    
    $stmt = $DB->prepare("CALL dbo.HisIsTheProcedureName(?,?,?)");
    if ($stmt) {
        $result = $stmt->execute([$val01, $val02, $val03]);
        if (!$result) {
            //look at error messages
        }
    } else {
        // look at error messages
    }