我不能用我的PHP脚本插入到MariaDB表
我最近搬到了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
我可以根据需要对引擎收录邮编,只问。
检查您的网络连接,然后再尝试这种方式,其做工精细:
//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()
;
这是有点相同我用...除了“setAttribute”为我得到一个错误。 – ranisalt 2013-05-04 20:34:32
你会得到什么错误?在这种情况下setAttribute设置为显示错误检查http://php.net/manual/en/pdo.setattribute.php更多关于它。 – konnection 2013-05-04 20:39:34
可能是它抛出的错误,为什么你的查询不插入! – konnection 2013-05-04 20:41:27
不是“PHP”,而是“*我的代码*不插入到MariaDB的表:”如果你想了解你的代码的codereivew – 2013-05-04 19:29:11
,它可能更适合于代码审查网站。这里的网站最适合具体的编程问题。 – hakre 2013-05-04 19:31:40
数据库中的所有被引用的列字符串? – 2013-05-04 19:32:59