如何从插入查询的PDO一般错误中获取正确的错误

时间:2014-08-07 07:36:48

标签: php mysql sql pdo error-handling

我试图进入某个表格,我得到了一般的错误。这是我的代码,如下所示。

注意:我在另一个更新查询中使用了这个连接,它工作正常。

另一件事,bindParam()和bindValue()之间的区别是什么?

$query = $dbh->query("INSERT INTO music SET
                      uid = ?,  title = ?,
                      album = ?, artist = ?,
                      year = ?, genre = ?,
                      albumart = ?, audio = ?,
                      time = ?, perms = ?
 ");    


$query->bindParam(1, $uid);
$query->bindParam(2, $title);
$query->bindParam(3, $album);
$query->bindParam(4, $artist);
$query->bindParam(5, $year);
$query->bindParam(6, $genre);
$query->bindParam(7, $art);
$query->bindParam(8, $song);
$query->bindParam(9, now());
$query->bindParam(10, $download);
$query->execute();

$check_exc = $query->execute();

if ($check_exc) {
    $new_id = $dbh->lastInsertId('sid');

感谢。

1 个答案:

答案 0 :(得分:0)

  1. 使用prepare代替query来电。
  2. 如果是PDO,请使用PDO错误处理程序而不是mysqli
  3. 对于bindParambindValue之间的差异,previous duplicate存在。
  4. $query = $dbh->prepare("INSERT INTO music SET
                          uid = ?,  title = ?,
                          album = ?, artist = ?,
                          year = ?, genre = ?,
                          albumart = ?, audio = ?,
                          time = ?, perms = ?");
    if( !$query ) {
        die( print_r($dbh->errorInfo()) );
    }