我的数据库在每个sql查询中插入2行

时间:2019-05-15 23:50:17

标签: php pdo

这是我的class.php代码:

<?php

/**
 * Class MrSharifi_DB
 */
final class MrSharifi_DB
{
    private $host;
    private $db_name;
    private $username;
    private $password;
    private $pdo;

    /**
     * MrSharifi_DB constructor.
     * @param array $options
     */
    public function __construct(array $options)
    {
        if (is_array($options)) {
            $this->host = $options["host"];
            $this->db_name = $options["db_name"];
            $this->username = $options["username"];
            $this->password = $options["password"];
            if (isset($this->host) and isset($this->db_name) and isset($this->username) and isset($this->password)) {
                $this->pdo = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name . ";", $this->username, $this->password);
                $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                $this->pdo->exec("SET CHARACTER SET utf8");
                $this->pdo->exec("SET NAMES utf8");
            }
        }

        return $this->pdo;
    }

    /**
     * @param $table
     * @param array|null $where
     * @param int $all
     * @param int $assoc
     * @param string $columns
     * @return array
     */
    public function select($table, array $where = null, $all = 1, $assoc = 1, $columns = "*")
    {
        $result = [];
        if (isset($table) and $table != "") {
            $sql = "SELECT " . $columns . " FROM `" . $table . "`";
            if (isset($where) and is_array($where)) {
                $sql .= " WHERE ";
                foreach ($where as $key => $value) {
                    $sql .= "`" . $key . "` = '" . $value . "' AND 1;";
                }
                $results = $this->pdo->query($sql);
                if ($all == 1) {
                    if ($assoc == 1) {
                        $result = $results->fetchAll(PDO::FETCH_ASSOC);
                    } else {
                        $result = $results->fetchAll(PDO::FETCH_NUM);
                    }
                } else {
                    if ($assoc == 1) {
                        $result = $results->fetch(PDO::FETCH_ASSOC);
                    } else {
                        $result = $results->fetch(PDO::FETCH_NUM);
                    }
                }
            } else {
                $sql .= ";";
                $results = $this->pdo->query($sql);
                $result = $results->fetchAll(PDO::FETCH_ASSOC);
            }
        }
        return $result;
    }


    public function insert($table, array $data)
    {
        $result = "";
        if (isset($table) and $table != "") {
            if (isset($data) and is_array($data) and $data != "") {
                $sql = "INSERT INTO `" . $table . "`";
                $columns = null;
                $values = null;
                foreach ($data as $key => $value) {
                    $columns .= $key . ", ";
                    $values .= "'" . $value . "', ";
                }
                $columns = rtrim($columns, ", ");
                $values = rtrim($values, ", ");
                $sql .= "(" . $columns . ") VALUES (" . $values . ");";
                self::debug($sql);
                $results = $this->pdo->exec($sql);
                self::debug($results);
            }
        }
        return $result;
    }

    /**
     * @param $param
     */
    public static function debug($param)
    {
        echo "<pre>";
        var_dump($param);
        echo "</pre>";
        die();
    }
}

当我像这样运行它时:

<?php
@require_once __DIR__ . "/class.php";

$connect = new MrSharifi_DB([
    "host" => "localhost",
    "db_name" => "simplephp",
    "username" => "root",
    "password" => "",
]);

$connect::debug($connect->insert("sphp_descriptions_buttons", [
    "btn_title" => "button title 343",
    "btn_text" => "button title 343 text"
]));

我的phpmyadmin两次运行此查询! 我不知道该怎么办! 请帮助...

  

我已经为btn_title设置了唯一性,但是它不起作用
  我也已经改变了使用绑定参数
  使用https://medoo.in时也遇到了这些问题...   这些问题只是在我尝试将其面向对象时发生的

0 个答案:

没有答案