doudianhuo1129 2014-04-03 16:37
浏览 39
已采纳

php数组到pdo变量

Here's what i'm trying to achieve: i'm learning OOP for school and i have tp make a registration thingy.... it's working but a lot is hardcoded and i'd like to make an universal insert function which works like $class->insert "1, 2, 3", "foo, bar, thing" where it'll insert foo into 1 bar into 2 and thing into 3 here comes the problem: i don't know how to convert my PHP array into SQL variables because i do not know how many items there are in my PHP array. (due to an explode)

here's my code so far:

<?php

 class database
{
static protected $_connection;

public function __construct($host, $dbname, $username, $password)
{
    self::$_connection = new PDO("mysql:host=" . $host . ";dbname=" . $dbname, $username, $password);
    self::$_connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    return self::$_connection;

}

public function insert($tablename, $columns, $values)
{
    $exploded = explode(", ", $values);
    $valuenumber = NULL;
    $string = NULL;
    $teller = 0;


    $query = self::$_connection->prepare('INSERT INTO gebruikers (:naam) VALUES ');
    foreach ($exploded as $array) {
        $query->bindparam(":value" . $valuenumber, $array);
        $valuenumber++;
    }
    for ($i = $valuenumber; $i > 0; $i--) {
        $string .= ", :value" . $teller;
        $teller++;
        // alles wordt in een string gezet: ":value, :value1, :value2
    }
    $stringarray = explode($string, ", ");
    $query->execute(array(
        'naam' => $teller
    ));
}

}



$client = new database("localhost", "oop", "root", "");
$client->insert("gebruikers", "naam", "test");

I'm sorry if it's a bit messy and for the lack of comments i'm pretty new in this.

Could anyone please help me out a bit? thanks,

g3.

  • 写回答

1条回答 默认 最新

  • dongluo8303 2014-04-03 17:04
    关注
    public function insert($table, $columns, $values)
    {
        // make sure we have the same amount of keys and values
        if(count($columns) !== count($values)){
            return false;
        }
    
        $query = self::$_connection->prepare('INSERT INTO ' . $table . ' (' . implode(',', $columns) . ') VALUES (:' . implode(',:', $columns) . ')');
    
        $index = 0;
        foreach($columns as $column){
            $query->bindparam(":" . $column . $values[$index++]);
        }
    
    }
    

    If I were you, I'd get rid of passing $columns and $values separately and just pass a key => pair array like:

    array('id' => 1, 'name' => 'new name');
    

    I understand this is a learning process for you're sort of re-inventing the wheel here. Check out libraries like doctrine.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧