Php 持牌人() { 返回“:”。内爆(“,:”,静态::getFieldNames()); } /** *getUpdateList-SQL更新部分 *@返回字符串 */ 静态私有函数getUpdateList() { $result=array(); for

Php 持牌人() { 返回“:”。内爆(“,:”,静态::getFieldNames()); } /** *getUpdateList-SQL更新部分 *@返回字符串 */ 静态私有函数getUpdateList() { $result=array(); for,php,mysql,pdo,bindparam,Php,Mysql,Pdo,Bindparam,持牌人() { 返回“:”。内爆(“,:”,静态::getFieldNames()); } /** *getUpdateList-SQL更新部分 *@返回字符串 */ 静态私有函数getUpdateList() { $result=array(); foreach(static::getFieldNames()作为$field){ 如果($field===static::getPrimaryKeyName())继续; $result[]='`.$field.`=:'.$field; } 返回内爆

持牌人() { 返回“:”。内爆(“,:”,静态::getFieldNames()); } /** *getUpdateList-SQL更新部分 *@返回字符串 */ 静态私有函数getUpdateList() { $result=array(); foreach(static::getFieldNames()作为$field){ 如果($field===static::getPrimaryKeyName())继续; $result[]='`.$field.`=:'.$field; } 返回内爆(“,”,$result); } /** *将字段绑定到PDO placeholds *@param PDOStatement$stmt字段绑定到的语句 *@返回无效 */ 受保护的函数绑定字段($stmt) { foreach(数组_键($this->fields)作为$field){ 如果($field===static::getPrimaryKeyName())继续; $stmt->bindParam(':'.$field,$this->fields[$field]); //回显$field.'->'.$this->fields[$field].
'; } } /** *将字段绑定到占位符 *@param PDOStatement$stmt-字段绑定到的 *@返回无效 */ 受保护的函数bindColumns($stmt,$withID=false) { 如果($withID) $stmt->bindColumn(static::getPrimaryKeyName(),$this->ID); foreach(静态::GetFieldName()为$fieldname) { $stmt->bindColumn($fieldname,$this->fields[$fieldname]); } } /** *解析结果集 *设置选择结果的值,重置脏(对象处于同步状态) *@param mixed[]$result-关联数组 */ 受保护的函数parseResultset($result) { foreach($结果为$字段=>$值){ 如果($field==static::getPrimaryKeyName()) $this->ID=$value; $this->fields[$field]=$value; } $this->dirty=array(); }
还请提及数组中的所有键,因为:b缺少“它只在循环执行的最后一个数据上工作”-请澄清这意味着什么。我的意思是不正确绑定我认为您覆盖了绑定的参数,您还必须执行$stmt->execute();在for循环中。那么我需要将execute()放在循环中吗?还请提及数组中的所有键,因为:b缺少“它只在循环执行的最后一个数据上工作”-请澄清这意味着什么。我的意思是不正确绑定我想你覆盖了bind的参数,你还必须执行$stmt->execute();在for循环中。那么我需要将execute()放在循环中吗?为什么使用
“更好”?您甚至可以将命名参数传递给
execute()
。为什么使用
“更好”?
?您甚至可以将命名参数传递给
execute()
。这应该是接受者的答案,但要注意命名参数的数量。使用动态循环不能很好地处理静态数量的命名参数。我的意思是,有时,params/value数组的值可能比查询中的命名参数多或少。应该是接受方的答案,但要注意命名参数的数量。使用动态循环不能很好地处理静态数量的命名参数。我的意思是,有时,params/value数组的值可能比查询中的命名参数多或少。。
$sql = "SELECT * FROM users WHERE id = :a OR fname = :b";

$array = array(":a"=>"10002345", "Josh");
$stmt = $conn->prepare($sql); 

foreach($array as $key => $value ) {
    $stmt->bindParam($key, $value);
}

$stmt->execute();
$sql = "SELECT * FROM users WHERE id = ? OR fname = ?";
$array = array("10002345", "Josh"); // you don't even need keys here
$stmt = $conn->prepare($sql); 
$stmt->execute($array);
foreach($array as $key => $value ) {
    $stmt->bindParam($key, $value);
}
foreach($array as $key => &$value ) {
    $stmt->bindParam($key, $value);
}
public function bindParam ($parameter, &$variable, $data_type = PDO::PARAM_STR, $length = null, $driver_options = null) {}
$sql = "SELECT * FROM users WHERE id = :a OR fname = :b";

$array = array(":a"=>"10002345", ":b"=>"Josh");

$stmt = $conn->prepare($sql); 

foreach($array as $key => $value ) {
    $stmt->bindParam($key, $value);
}

$stmt->execute();
/**
 * getFieldList return the list with or without PK column
 * @param bool $withID - true when including parameter
 */
static protected function getFieldList( $withID = false )
{
    if( $withID )
        $result = '`' . static::getTableName( ) . '`' .
            '.`' . static::getPrimaryKeyName( ) . '`, ';
    else
        $result = '';

    return $result .= '`' . static::getTableName( ) . '`.' . 
        '`' . implode( '`, `'.static::getTableName( ) . '`.`', static::getFieldNames( ) ) . '`';
}

/**
 * getFieldPlaceholders - 
 * @return string - all PDO place holders prefixed :
 */
static protected function getFieldPlacholders( )
{
    return ':' . implode( ',:', static::getFieldNames( ) );
}

/**
 * getUpdateList - SQL updates section
 * @return string
 */
static private function getUpdateList( )
{
    $result = array( );
    foreach( static::getFieldNames( ) as $field ) {
        if( $field === static::getPrimaryKeyName() ) continue;
        $result[] = '`' . $field . '`=:' . $field;
    }
    return implode( ',', $result );
}

/**
 * Bind the fields to PDO placeholdes
 * @param PDOStatement $stmt statement that the fields are bound to
 * @return void
 */
protected function bindFields( $stmt )
{
    foreach( array_keys($this->fields) as $field ) {
        if( $field === static::getPrimaryKeyName() ) continue;
        $stmt->bindParam( ':' . $field, $this->fields[$field] );

        // echo $field . '->' . $this->fields[$field] . '<br>';
    }
}
/**
 * Bind the fields to the placeholders
 * @param PDOStatement $stmt - that the fields are bind to
 * @return void
 */
protected function bindColumns( $stmt, $withID = false )
{
    if( $withID )
        $stmt->bindColumn( static::getPrimaryKeyName(), $this->ID );
    foreach( static::getFieldNames() as $fieldname )
    {
        $stmt->bindColumn( $fieldname, $this->fields[$fieldname] );
    }   
}

/**
 * parseResultset
 * Set the values of the select results, resets dirty (object is in sync)
 * @param mixed[] $result - associative array
 */
protected function parseResultset( $result )
{
    foreach( $result as $field=> $value ) {
        if( $field === static::getPrimaryKeyName() )
            $this->ID = $value;
        $this->fields[$field] = $value;
    }
    $this->dirty = array();
}