: This article mainly introduces the differences between the bindParam and bindValue methods of the PDOStatement class in phppdo. if you are interested in the PHP Tutorial, refer to it. The two methods in the PDOStatement class are described as follows:
Bool PDOStatement: bindParam (mixed $ parameter, mixed & $ variable [, int $ data_type = PDO: PARAM_STR [, int $ length [, mixed $ driver_options])bool PDOStatement::bindValue ( mixed $parameter , mixed $value [, int $data_type = PDO::PARAM_STR ] )
Difference 1: bindParam binds a parameter to the specified variable name, while bindValue binds a value to a parameter.
$db = new PDO('mysql:host=localhost;dbname=dbtest;charset=utf8','user','pass');$st = $db->prepare('select * from tabletest where id = ?');$id = 1;$st->bindParam(1,$id,PDO::PARAM_INT);//$st->bindValue(1,$id,PDO::PARAM_INT);
In the above code, both bindParam and bindValue can be executed normally.$db = new PDO('mysql:host=localhost;dbname=dbtest;charset=utf8','user','pass');$st = $db->prepare('select * from tabletest where id = ?');$st->bindParam(1,1,PDO::PARAM_INT);//$st->bindValue(1,1,PDO::PARAM_INT);
BindParam reports the following error, but bindValue can be executed normally.
Fatal error: Cannot pass parameter 2 by reference
Conclusion: bindParam has only one variable name but not a specific value. bindValue can bind both a variable name and a value.
Difference 2: different from PDOStatement: bindValue (), PDOStatement: variables in bindParam () are bound as references and only in PDOStatement: execute () the value is obtained only when it is called.
$db = new PDO('mysql:host=localhost;dbname=dbtest;charset=utf8','user','pass');$st = $db->prepare('select * from tabletest where id = ?');$id = 1;$st->bindParam(1,$id,PDO::PARAM_INT);$id = 2;$st->execute();$rs = $st->fetchAll();print_r($rs);
First, assign $ id to 1, bindParam to bind the variable, Change $ id to 2 before execute, and then perform the operation, the result set obtained at this time is the query result when id = 2, not the query result when id is 1. this is the reference interpretation of the variable. before execute, we can replace this variable, and the variable value entered during the execute operation is the last change value of this variable.$db = new PDO('mysql:host=localhost;dbname=dbtest;charset=utf8','user','pass');$st = $db->prepare('select * from tabletest where id = ?');$id = 1;$st->bindValue(1,$id,PDO::PARAM_INT);$id = 2;$st->execute();$rs = $st->fetchAll();print_r($rs);
BindValue is different. after bindValue is used to bind a variable, the result will not change even if the variable value is changed before execute. For example, in the above example, even if we change $ id to 2, the final execution result will still output the result at $ id = 1, because bindValue is not bound to a variable reference, does not change as the variable changes.Although both of them can bind SQL parameters, there are still differences between the two. in actual application, we should choose the one that suits us. The following is an example of improper use of bindParam.
Assume that a data table has two fields: integer id and string name, and an array of data $ params = array (1, 'Zhang San') is prepared to be inserted using preprocessing. the specific code is as follows:
$ Db = new PDO ('MySQL: host = localhost; dbname = dbtest; charset = utf8', 'user', 'pass '); $ st = $ db-> prepare ('Insert into tabletest (id, name) values (?,?) '); $ Params = array (1, 'Zhang San'); foreach ($ params as $ k => $ v) {$ index = $ k + 1; $ st-> bindParam ($ index, $ v) ;}$ st-> execute ();
The SQL statement that is executed normally isInsert into tabletest (id, name) values (1, 'Zhang San ');
The actually executed SQL statement isInsert into tabletest (id, name) values ('male', 'male ');
The reason is that the variable in bindParam is bound as a reference, so the value inserted for each field is changed to the value of the last field, at this time, we will not encounter this problem when using bindValue. In this example, note that if you use a question mark placeholder and an index array, pay special attention to the parameter identifier of bindValue (the first parameter of this method ), the index array starts from 0 by default, while the parameter identifier of bindValue starts from 1. if the index array is directly set to 0 subscript, the program will report an error. Be sure to pay attention when using the index array.
The above introduces the differences between the bindParam and bindValue methods of the PDOStatement class in php pdo, including some content. I hope my friends who are interested in PHP tutorials can help me.