PDOStatement::bindParam

(PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 0.1.0)

PDOStatement::bindParam — ��һ��������ָ���ı�����

˵��

PDOStatement::bindParam ( mixed $parameter , mixed &$variable [, int $data_type = PDO::PARAM_STR [, int $length [, mixed $driver_options ]]] ) : bool

��һ��PHP����������Ԥ�����SQL����еĶ�Ӧ����ռλ�����ʺ�ռλ���� ��ͬ�� PDOStatement::bindValue() ���˱�����Ϊ���ñ��󶨣���ֻ�� PDOStatement::execute() �����õ�ʱ���ȡ��ֵ��

������������������������������ֻ���ķ�ʽ����������ѯ��һЩ����֧�ֵ��ô洢���̲���Ϊ��������������ݣ�һЩ֧����Ϊ����/����������ȷ��������ֽ��ո��º�����ݡ�

����

parameter

������ʶ��������ʹ������ռλ����Ԥ������䣬Ӧ������ :name ��ʽ�IJ�����������ʹ���ʺ�ռλ����Ԥ������䣬Ӧ����1��ʼ�����IJ���λ�á�

variable

�󶨵� SQL �������� PHP ��������

data_type

ʹ�� PDO::PARAM_* ������ȷ��ָ�����������͡�Ҫ��һ���洢�����з���һ�� INOUT ��������ҪΪ data_type ����ʹ�ð�λ�������ȥ���� PDO::PARAM_INPUT_OUTPUT λ��

length

�������͵ij��ȡ�Ϊ����������һ���洢���̵� OUT ������������ȷ�����ô˳��ȡ�

driver_options

����ֵ

�ɹ�ʱ���� TRUE�� ������ʧ��ʱ���� FALSE��

����

Example #1 ִ��һ��ʹ������ռλ����Ԥ�������

<?php
/* ͨ���󶨵� PHP ����ִ��һ��Ԥ�������  */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
    FROM fruit
    WHERE calories < :calories AND colour = :colour'
);
$sth->bindParam(':calories', $calories, PDO::PARAM_INT);
$sth->bindParam(':colour', $colour, PDO::PARAM_STR, 12);
$sth->execute();
?>

Example #2 ִ��һ��ʹ���ʺ�ռλ����Ԥ�������

<?php
/*  ͨ���󶨵� PHP ����ִ��һ��Ԥ������� */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
    FROM fruit
    WHERE calories < ? AND colour = ?'
);
$sth->bindParam(1, $calories, PDO::PARAM_INT);
$sth->bindParam(2, $colour, PDO::PARAM_STR, 12);
$sth->execute();
?>

Example #3 ʹ�� INOUT ��������һ���洢����

<?php
/* ʹ�� INOUT ��������һ���洢���� */
$colour = 'red';
$sth = $dbh->prepare('CALL puree_fruit(?)');
$sth->bindParam(1, $colour, PDO::PARAM_STR|PDO::PARAM_INPUT_OUTPUT, 12);
$sth->execute();
print(
"After pureeing fruit, the colour is: $colour");
?>

�μ�