0

I am new to PDO, I know with execute you send the value as a string, but when you bind param you can define what it is, but no mater what my queries always work when sending them via a string via an execute, so what is the point (there is one I am just trying to figure what is it) of using bind param.

For example:

$insert_whatever = $database->prepare("INSERT INTO test(blah, blah1) VALUES (?, ?)");
$insert_whatever->bindparam('1', PDO::PARAM_INT);
$insert_whatever->execute(); 


$insert_whatever = $database->prepare("INSERT INTO test(blah, blah1) VALUES (?, ?)");
$insert_whatever->execute(array('1','1')); 
Iamnotyou
  • 3
  • 3

1 Answers1

0

Execute fails sometimes. For example if you want to LIMIT your results returned from MySQL, then you will need to Bind parameters / values instead of passing them with execute command.

i.e.

$query = $pdo->prepare("SELECT * FROM table LIMIT ?, ?");

You will do:

$query->bindParam("1", 1, PDO::PARAM_INT) // assuming that you want to start from 1
$query->bindParam("2", 10, PDO::PARAM_INT) // assuming that you want to stop at 10

and then execute:

$query->execute();

If you are going to attach an integer value from a variable, then you will need to bind it as a value:

$query->bindValue("1", $variable, PDO::PARAM_INT);

Hint: bindValue works for all. Learn more about bindParam and bindValue here: What is the difference between bindParam and bindValue?

Community
  • 1
  • 1
Rehmat
  • 4,681
  • 3
  • 22
  • 38