This question already has an answer here:
这个问题已经有了答案:
- My PDO Statement doesn't work 1 answer
- 我的PDO语句不能工作1个答案
I really have NO idea of what to do with this now, i have been staring at it for hours, and reqritten it.. i can't get it to work!.
我真的不知道该怎么处理这件事,我已经盯着它看了好几个小时了。我不能让它工作!
require_once("Abstracts/DBManager.php");
require_once("UI/UI.Package.php");
class BlogDBM extends DBManager
{
private $table = "blog_records";
function saveRecord($title,$url,$desc,$feedId,$pubDate)
{
$PDO = $this->db->connect();
try
{
$query = $PDO->prepare("
INSERT INTO ".$this->table."
(title,url,desc,feed_id,pubdate) VALUES
(:title,:url,:desc,:feed_id,:pubdate)");
$query->bindParam(":title", $title);
$query->bindParam(":url", $url);
$query->bindParam(":desc", $desc);
$query->bindParam(":feed_id", $feedId, PDO::PARAM_INT);
$query->bindParam(":pubdate", $pubDate, PDO::PARAM_INT);
$query->execute();
//return $PDO->lastInsertId();
} catch(PDOException $e)
{
echo "Error " . $e->getMessage();
}
$PDO = NULL;
}
}
6 个解决方案
#1
16
I'm pretty sure that mySQL chokes on the desc
field name - it is a reserved word. You'd have to put it into "`" quotes or, better, change the field name.
我很确定mySQL在desc字段名上的选择-它是一个保留字。你必须把它放到“'”引号中,或者,更好的,更改字段名。
As for error reporting, use the errorInfo method. You can make PDO actually output the result of a failed query in the exception, but the default behaviour - I think - is to throw an exception only if the query can't be made at all, but it doesn't fail if the query is faulty.
至于错误报告,请使用errorInfo方法。您可以让PDO在异常中实际输出失败查询的结果,但默认行为(我认为)是,只有在查询完全无法执行时才抛出异常,但如果查询有错误,则不会失败。
#2
36
Just wanted to add to this, had similar frustrations from the lack of an error message.
只是想补充一点,由于缺少错误消息,也有类似的挫折。
To stop PDO from silently failing, you can set the error mode on the PDO connection.
若要停止PDO的静默失败,可以在PDO连接上设置错误模式。
$dbh = new PDO();
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
There is also PDO::ERRMODE_WARNING
if you want errors but still continue.
也有PDO::ERRMODE_WARNING如果您想要错误,但是仍然继续。
#3
3
I was also facing that error.
我也面临着这个错误。
I used print_r($con->errorInfo());
it gives me 0000
in 0th
key of array.
我使用print_r($ con - > errorInfo());它给了我0000的数组第0个键。
Then I matched all column names and figured out that I am using wrong field name.
然后我匹配了所有的列名,发现我使用了错误的字段名。
This saves my day.
这节省了我的一天。
#4
2
It also happens when you use PDOStatement::bindValue()
with PDO::PARAM_BOOL
. Solution: just switch to PDO::PARAM_INT
.
当您使用PDOStatement::bindValue()和PDO:::PARAM_BOOL时也会发生。解决方案:只需切换到PDO::PARAM_INT。
#5
0
Similar problems can occur when someone turns/leaves off the DB autoincrement on the main id field.
当某人在主id字段上关闭/关闭DB自动增量时,也会发生类似的问题。
#6
-1
i struggled with this silent insert fail this week. and here's the solution that worked for me. i was not calling commit on the transaction, so the insert was put into the pending state but was never completed on the database - hence no error. this was trickier because the PDR db wrapper in this project is a static class so it doesn't automatically close
这周,我一直在为这个无声的插入而挣扎。这就是我的解决方案。我没有在事务上调用commit,因此插入被放到pending状态,但是在数据库上从来没有完成—因此没有错误。这比较复杂,因为这个项目中的PDR db包装器是一个静态类,所以不会自动关闭
solution: make sure to call commit on the PDO handle after the insert / update / delete actions - or on page close.
解决方案:确保在插入/更新/删除操作之后,在PDO句柄上调用commit——或者在页面关闭时。
$PDO->exec("COMMIT;");
$ PDO - > exec(“承诺;”);
#1
16
I'm pretty sure that mySQL chokes on the desc
field name - it is a reserved word. You'd have to put it into "`" quotes or, better, change the field name.
我很确定mySQL在desc字段名上的选择-它是一个保留字。你必须把它放到“'”引号中,或者,更好的,更改字段名。
As for error reporting, use the errorInfo method. You can make PDO actually output the result of a failed query in the exception, but the default behaviour - I think - is to throw an exception only if the query can't be made at all, but it doesn't fail if the query is faulty.
至于错误报告,请使用errorInfo方法。您可以让PDO在异常中实际输出失败查询的结果,但默认行为(我认为)是,只有在查询完全无法执行时才抛出异常,但如果查询有错误,则不会失败。
#2
36
Just wanted to add to this, had similar frustrations from the lack of an error message.
只是想补充一点,由于缺少错误消息,也有类似的挫折。
To stop PDO from silently failing, you can set the error mode on the PDO connection.
若要停止PDO的静默失败,可以在PDO连接上设置错误模式。
$dbh = new PDO();
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
There is also PDO::ERRMODE_WARNING
if you want errors but still continue.
也有PDO::ERRMODE_WARNING如果您想要错误,但是仍然继续。
#3
3
I was also facing that error.
我也面临着这个错误。
I used print_r($con->errorInfo());
it gives me 0000
in 0th
key of array.
我使用print_r($ con - > errorInfo());它给了我0000的数组第0个键。
Then I matched all column names and figured out that I am using wrong field name.
然后我匹配了所有的列名,发现我使用了错误的字段名。
This saves my day.
这节省了我的一天。
#4
2
It also happens when you use PDOStatement::bindValue()
with PDO::PARAM_BOOL
. Solution: just switch to PDO::PARAM_INT
.
当您使用PDOStatement::bindValue()和PDO:::PARAM_BOOL时也会发生。解决方案:只需切换到PDO::PARAM_INT。
#5
0
Similar problems can occur when someone turns/leaves off the DB autoincrement on the main id field.
当某人在主id字段上关闭/关闭DB自动增量时,也会发生类似的问题。
#6
-1
i struggled with this silent insert fail this week. and here's the solution that worked for me. i was not calling commit on the transaction, so the insert was put into the pending state but was never completed on the database - hence no error. this was trickier because the PDR db wrapper in this project is a static class so it doesn't automatically close
这周,我一直在为这个无声的插入而挣扎。这就是我的解决方案。我没有在事务上调用commit,因此插入被放到pending状态,但是在数据库上从来没有完成—因此没有错误。这比较复杂,因为这个项目中的PDR db包装器是一个静态类,所以不会自动关闭
solution: make sure to call commit on the PDO handle after the insert / update / delete actions - or on page close.
解决方案:确保在插入/更新/删除操作之后,在PDO句柄上调用commit——或者在页面关闭时。
$PDO->exec("COMMIT;");
$ PDO - > exec(“承诺;”);