-1

我最近搬到了 MariaDB,因为自 MySQL 5.6 以来它对我来说已经失败了很多。

MariaDB 工作完美,但在一个新项目中,我无法使用 PHP 脚本将数据插入数据库。我只能手动插入。我没有对使用 MySQL 的脚本进行任何更改。

INSERT 语句是:

INSERT INTO participantes (nome, curso, email, equipe) VALUES (:nome, :curso, :email, :equipe);

应该插入的脚本是:

$stmt = $this->dbh->prepare($this->SQL_INSERT);
$nome = $participante->nome();
$curso = $participante->curso();
$email = $participante->email();
$equipe = $participante->equipe();
$stmt->bindParam(':nome', $nome);
$stmt->bindParam(':curso', $curso);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':equipe', $equipe);
$stmt->execute();

“参与者”函数返回要使用的数据,没有问题。一切都在一个 try/catch 块内,它不会报告任何异常。

我的 PDO 课程如下:

class Connection extends PDO {
    private $dsn = 'mysql:host=localhost;port=3307;dbname=dacu';
    private $usr = 'dacu';
    private $pwd = 'my password';

    public $handle = null;

    function __construct() {
        try {
            if ($this->handle == null) {
                $dbh = new PDO($this->dsn, $this->usr, $this->pwd);
                $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                $this->handle = $dbh;
                return $this->handle;
            }
        }
        catch (PDOException $e) {
            throw new Exception('Não foi possível conectar-se ao banco de dados: ' . $e->getMessage());
        }
        catch (Exception $e) {
            throw new Exception('Um erro não identificado ocorreu: ' . $e->getMessage());
        }
    }
}

使用 controller->insert 给我:

Warning: PDO::prepare(): SQLSTATE[00000]: No error: PDO constructor was not called in C:\Webserver\Files\dacu\controller\EquipesController.php on line 25

如有必要,我可以在 pastebin 上发布代码,请问。

4

1 回答 1

1

检查您的连接,然后尝试这种方式,它工作正常:

    //first you need a connection to mysql use this and replace the variables

        try {
          $dbh = new PDO('mysql:host=localhost;dbname='.$db_name.'', $user, $pass, array(
            PDO::ATTR_PERSISTENT => true
        ));
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
          } catch (PDOException $e) {
            print "Error!: " . $e->getMessage() . "<br/>";
            die();
        }

    //then do the insert variable

        $sql  = 'INSERT INTO encomendas (`nome`, `user`, `id`, `email`) ';
        $sql .= 'VALUES (:nome, :curso, :email, :equipe )';


        $query = $dbh->prepare($sql);

    //bindParam with your own variables

        $query->bindParam(':nome', $varDoNome, PDO::PARAM_STR);
        $query->bindParam(':curso',  $varDoCurso, PDO::PARAM_STR);
        $query->bindParam(':email', $varDoEmail, PDO::PARAM_STR);
        $query->bindParam(':equipe', $varDaequipe, PDO::PARAM_STR);

    //execute query

        $query->execute()

;
于 2013-05-04T20:16:52.270 回答