Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php PDO:使execute语句动态…但不工作_Php_Pdo - Fatal编程技术网

Php PDO:使execute语句动态…但不工作

Php PDO:使execute语句动态…但不工作,php,pdo,Php,Pdo,这应该很容易…但我想不出来。如果以下execute语句在insert查询结束时起作用 $query->execute(array($this->visible_password, $this->hashed_password, $this->temp_hashed_password, $this-

这应该很容易…但我想不出来。如果以下execute语句在insert查询结束时起作用

     $query->execute(array($this->visible_password, 
                           $this->hashed_password, 
                           $this->temp_hashed_password, 
                           $this->first_name, 
                           $this->last_name, 
                           $this->position, 
                           $this->location, 
                           $this->city, 
                           $this->email, 
                           $this->country, 
                           $this->institution, 
                           $this->interests, 
                           $this->profile_comment));

给予

$this->visible\u password,$this->hash\u password,$this->temp\u hash\u password,$this->email,$this->first\u name,$this->last\u name,$this->position,$this->location,$this->city,$this->country,$this->institution

那为什么这不起作用

     $query->execute(array($result))
占位符上的var_转储提供

   array(13) { 

 [0]=> string(26) "$this->visible_password" 
 [1]=> string(25) "$this->hashed_password" 
 [2]=> string(30) "$this->temp_hashed_password" 
 [3]=> string(15) "$this->email" 
 [4]=> string(20) "$this->first_name" 
 [5]=> string(19) "$this->last_name" 
 [6]=> string(18) "$this->position" 
 [7]=> string(18) "$this->location" 
 [8]=> string(14) "$this->city" 
 [9]=> string(17) "$this->country" 
 [10]=> string(21) "$this->institution" 
 [11]=> string(19) "$this->interests" 
 [12]=> string(25) "$this->profile_comment" }
$placeholder是一个数组…它接受类的属性($attributes)

            $place_holder = array();
            foreach ($attributes as $key => $value) {
            $place_holder[] = "&#36this->" . $key;
        }

我一直在尝试在我的用户类抽象中创建用户方法,这样我就可以在我的所有类中使用它。我一直在把我的网站从mysqli(噩梦)转换成PDO。这里是方法

public function  pdo_create_test() {

  $attributes = $this->attributes();
  $att = array_keys($attributes);     
   $question_marks = array();
    foreach ($attributes as $key => $value) {
            $question_marks[] = "?";
    }
   $place_holder = array();
    foreach ($attributes as $key => $value) {
           $place_holder[] = "&#36this->" . $key;
        }
  $result = join(", ", array_values($place_holder));
  $sql = "INSERT INTO ".self::$table_name." (";
  $sql .= join(", ", array_keys($attributes));
  $sql .= ") VALUES (";
  $sql .= join(", ", array_values($question_marks));
  $sql .= ")";
  $query = $handler->prepare($sql);
  $query->execute(array($this->visible_password, 
                      $this->hashed_password, 
                      $this->temp_hashed_password, 
                      $this->first_name, 
                      $this->last_name, 
                      $this->position, 
                      $this->location, 
                      $this->city, 
                      $this->email, 
                      $this->country, 
                      $this->institution, 
                      $this->interests, 
                      $this->profile_comment));

                   }
这是一个更新…(我已经更改了占位符以获得比尔在下面所说的值) 我还回显了sql和占位符的结果,以查看所显示的内容

public function  pdo_create_test() {
  global $handler;
  $attributes = $this->attributes();
  $att = array_keys($attributes);
  $question_marks = array();
    foreach ($attributes as $key => $value) {
    $question_marks[] = "?";
    }
  $place_holder = array();
    foreach ($attributes as $key => $value) {
    $place_holder[] = $this->$key;
        }
  $result = join(", ", array_values($place_holder));
  $sql = "INSERT INTO ".self::$table_name." (";
  $sql .= join(", ", array_keys($attributes));
  $sql .= ") VALUES (";
  $sql .= join(", ", array_values($question_marks));
  $sql .= ")";
    echo $sql;
    echo "<br/>";
    echo "<br/>";
    echo $result;
    $query = $handler->prepare($sql);
    $query->execute(array($result));

}
输出是

插入用户(可见密码、散列密码、临时散列密码、电子邮件、名字、姓氏、职位、位置、城市、国家、机构、兴趣、个人资料注释)值(?,,,,,,,,,,,,?)

山姆,沃尔什


但是没有进入数据库…不明白为什么…其他DB字段被设置为NULL,所以这不是问题….

我不确定您的
place\u holder
变量包含什么,但我认为这可能是原因。只是返回字符串的函数的另一个名称。Execute需要实际的物理输入项

从上的PHP站点获取输入值数组:

值的数组,其元素数与绑定参数数相同 在正在执行的SQL语句中。所有值都被视为 PDO::PARAM_STR

更新

如注释中所述,这里有一个函数,它将使用给定的数据对象执行SQL字符串。最好将其分离为几个函数(创建参数数组、绑定参数等),但为了可读性,我将它们全部放在一个函数中

函数的一个示例使用可以是
executeQuery('SELECT*FROM users,其中id=:id',array('id'=>1))


PHP确实具有的概念,因此您可以这样做:

$varname = 'foo';

$foo = 123;

echo $$varname;
但这一特点是有限的。不能将其应用于数组的所有元素,也不能将其与对象变量的
$this->foo
符号一起使用

可以使用变量语法通过以下方式获取与属性对应的对象变量值:

$place_holder = array();
foreach ($attributes as $key => $value) {
    $place_holder[] = $this->$key;
}
注意
->
之后的
$key
。通常,对象变量语法在该位置没有
$
。通过在那里使用
$
,我告诉它获取对象变量的值,该变量的名称是
$key
的值

顺便说一句,这里有一个更简洁(可能更快)的方法来做同样的事情:

$place_holder = array_intersect_key($attributes, get_object_vars($this));

请回复您的评论:

首先,你做错了:

$result = join(", ", array_values($place_holder));
这将生成一个字符串,其值用逗号分隔。如果您将其包装在
数组($result)
中,这并不重要,因为这只会创建一个包含单个字符串的数组,即使单个字符串包含逗号

换言之,这两个阵列完全不同:

array('A,B,C') // array with one element

array('A', 'B', 'C') // array with three elements
相反,您需要传递给
execute()
的是一个数组,其元素数与
占位符数相同

因此,只需传递以下信息:

$query->execute($place_holder);
其次,您需要在每次
prepare()
execute()
之后检查错误:

或者,如果这似乎是太多的编码,你可以改为,一个错误将导致你的应用程序终止与一个错误

$handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$place\u holder
包含什么?发布
var\u dump($place\u holder)的输出
。您的
$place\u holder
值是字符串而不是变量,因此您的
join()
也返回字符串而不是变量。您如何定义
$place\u holder
?理解,但肯定有一种方法可以使其动态化,不是吗?您想要什么样的动态功能?我将发布一些我在一些项目中使用过的东西,这些东西使它变得更简单。我一直在尝试在我的用户类中抽象创建用户方法,以便我可以在我的所有类中使用它。我一直在将我的网站从mysqli(噩梦)转换为PDO。@GhostRider我已经用一个示例函数更新了我的答案,以动态地将项目绑定到给定的查询。好的,我将对此进行研究并尝试一下。抱歉,如果我需要一些时间才能返回结果…Bill尝试了这两种方法…请参见我在上面的下方编辑…输出表明输入了正确的值,但数据库中什么也没有发生…您真是个天才…尽管我必须使用array_values()将数组转换为索引数组。谢谢你的错误提示…我将实现这些。
array('A,B,C') // array with one element

array('A', 'B', 'C') // array with three elements
$query->execute($place_holder);
if (($query = $handler->prepare(...)) === false) {
  print_r($handler->errorInfo());
}
if ($query->execute($result) === false) {
  print_r($query->errorInfo());
}
$handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);