doukun8670 2018-05-30 03:42
浏览 39

插入数据库不起作用[重复]

This question already has an answer here:

I tried to simplify this as much as possible just to understand what's happening but it's still not working. It doesn't show any errors, but nothing gets added to database.

The bootstrap file:

class Connection {
    public static function make($config){
        return new PDO(
            $config['connection'].';dbname='.$config['name'],
            $config['username'], $config['password'], $config['options']
        );  
    }
}

class QueryBuilder {
    protected $pdo;

    public function __construct($pdo){
        $this->pdo = $pdo;
    }

    public function insert($table, $paramater, $value){     
        $statement = $this->pdo->prepare("insert into $table ($paramater) values($value)");
        $statement->execute();
    }
}

return new QueryBuilder(Connection::make($config['database']));

Other file after post request:

$query = require 'bootstrap.php';

$query->test('todos', 'description', 'lorem ipsum');

My $config array is this

return [
  'database' => [
    'name' => 'mytodo', 
    'username' => 'root', 
    'password' => '', 
    'connection' => 'mysql:host=ip', 
    'options' => [ ] 
  ]
]; 
</div>
  • 写回答

1条回答 默认 最新

  • dptj13337 2018-05-30 03:46
    关注

    According to the manual at http://php.net/manual/en/pdo.connections.php, your connection string is missing "mysql:".

    Try:

    return new PDO("mysql:".$config['connection'] etc.

    Also, try putting a try-catch block around it and display the message from whatever Exception it may be throwing.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 被蓝屏搞吐了,有偿求帮解答,Ai回复直接拉黑
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并