PHP + PDO: bind zero if parameter is empty

I am trying to do this (and all PoST variables are processed before the user submits them, no problem with SQL Injection):

$stmt = $con->prepare($sql);
$stmt->bindParam(":1", $this->getPes_cdpessoa());
$stmt->bindParam(":2", $this->getPdf_nupessoa_def());

When any of these vars is NULL, PDO cries and does not allow my statement to be executed, and in my table I allow these fields to be assigned nullables.

Is there a way to check if the values ​​are empty, pdo just binds NULL to that (and I mean, a smart way instead if (empty ($ _ POST ['blablabla') ...) for each individual parameter?

+3
source share
3 answers

Try:

$stmt = $con->prepare($sql);
$stmt->bindParam(':1', $this->getPes_cdpessoa(), PDO::PARAM_NULL);
$stmt->bindParam(":2", $this->getPdf_nupessoa_def(), PDO::PARAM_NULL);

Also see:

+5

bindParam , , . , null, bindParam .

bindValue. , bindValue , , bindParam , .

+2

Alternative syntax works:

$stmt = $con->prepare($sql);
$stmt->execute(array($this->getPes_cdpessoa(), $this->getPdf_nupessoa_def()));
0
source

Source: https://habr.com/ru/post/1761972/