2013-05-04 20 views
-1

我最近搬到了MariaDB,因爲自從MySQL 5.6以來,它對我來說已經失敗了很多。我不能用我的PHP腳本插入到MariaDB表

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(); 

的 「PARTICIPANTE」 函數返回要使用的數據,沒有任何問題。所有內容都位於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()); 
     } 
    } 
} 

而且使用控制器 - >插入給我:

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

我可以根據需要對引擎收錄郵編,只問。

+4

不是「PHP」,而是「*我的代碼*不插入到MariaDB的表:」如果你想了解你的代碼的codereivew – 2013-05-04 19:29:11

+0

,它可能更適合於代碼審查網站。這裏的網站最適合具體的編程問題。 – hakre 2013-05-04 19:31:40

+0

數據庫中的所有被引用的列字符串? – 2013-05-04 19:32:59

回答

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() 

; 
+0

這是有點相同我用...除了「setAttribute」爲我得到一個錯誤。 – ranisalt 2013-05-04 20:34:32

+0

你會得到什麼錯誤?在這種情況下setAttribute設置爲顯示錯誤檢查http://php.net/manual/en/pdo.setattribute.php更多關於它。 – konnection 2013-05-04 20:39:34

+1

可能是它拋出的錯誤,爲什麼你的查詢不插入! – konnection 2013-05-04 20:41:27

相關問題