douduxia1551 2014-09-27 19:39
浏览 46
已采纳

在函数pdo中输入数组

I'm trying to make a unified function for a PDO prepare UPDATE statement. But I am doing something wrong. Help would be very much appreciated.

I'm passing an array and an email to a function:

function refreshUserData($items,$email)
{
    global $pdo;

    $keys = array_keys($items);
    $keys = '('. implode(",", $keys) . ')';

    $values = array_values($items);
    $values = '(\''. implode("','", $values) . '\')';

    $query = $pdo->prepare("UPDATE users SET $keys VALUES $values WHERE email=?");
    $query->bindValue(1,$email);
    $query->execute();
}

But I am doing something wrong because when I try to run a function Uncaught exception

This is my first attempt at doing this so don't know what to do.

Array being passed through:

Array ( [google_id] => 11111111111111 [emal] => email@gmail.com [first_name] => First [last_name] => Last [profile_url] => https://plus.google.com/+FirstLast )

And after implodes:

(google_id,emal,first_name,last_name,profile_url)

('11111111111111','email@gmail.com','First','Last','https://plus.google.com/+FirstLast')
  • 写回答

1条回答 默认 最新

  • doude4924 2014-09-27 19:56
    关注

    You have an error in your syntax UPDATE syntax is always like:

    UPDATE tablename
    SET columname = 'value', othercolumn = 'othervalue'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?