-1

I recently moved to MariaDB, because since MySQL 5.6 it has been failing a lot for me.

MariaDB works flawlessly but, in a new project, I cannot insert data to the database with a PHP script. I can only insert manually. I have made no changes from scripts working with MySQL.

The INSERT statement is:

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

And the script which should be inserting is:

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

The "participante" functions return data to be used, with no problems. Everything is inside a try/catch block, which reports no exceptions.

My PDO class is as follows:

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

And using controller->insert gives me:

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

I can post code on pastebin if necessary, just ask.

ranieri
  • 2,030
  • 2
  • 21
  • 39

1 Answers1

1

Check your connection, then try this way, its working fine:

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

;
konnection
  • 433
  • 4
  • 13
  • It's kinda the same I use... except "setAttribute" gets an error for me. – ranieri May 04 '13 at 20:34
  • what error do you get? the setAttribute in that case is setting to display errors check http://php.net/manual/en/pdo.setattribute.php for more about it. – konnection May 04 '13 at 20:39
  • 1
    probably the error that it throws, is why your query dont insert! – konnection May 04 '13 at 20:41
  • 1
    but the user Your Common Sense has already pointed that and as very good response in the fist comments!! check that out! – konnection May 04 '13 at 21:02
  • I tested every line of code again and found the error. There are no PDO/DB errors anymore, this piece of code solved the problem. The fault is now on my code, already fixed! – ranieri May 04 '13 at 23:32