开发者

When preparing a query, how important is it to check whether ($stmt = $mysqli->prepare($query)) is false?

Is this just an issue when there's a problem connecting to the database? (I do check to see if the database connection was successful). Assuming that I do enough checks to ensure that my queries are always valid, is it ever okay to skip the following check:

if($stmt = $db->prepare($query)) { //more code }

and instead just do:

$stmt = $db->prepare($query); //more code

I'm thinking that transactions might make this okay, but my database tables doesn't support transactions (I'm using MyISA开发者_开发问答M because I'm doing a lot more db reading than writing).

Thanks!


Tandu provides a severely misleading notion here, which you've unfortunately accepted as an answer.

Regardless of what database abstraction layer you're using (you're using mysqli), it is actually reliant on whether or not the prepared query is emulated, or if it's natively run at the database level. The prepared query can, and should fail, if the underlying database engine fails to "prepare" it.

It has nothing to do with whether or not your database connection was successful.

You absolutely should check the return value of the prepare() call. If you have a programming bug, it will fail. If you have another level of application problem, it will fail. Etc. That's the entire point of the return value.

If you assume it's always good, you're risking a failed query, which would result in loss of data - potentially more.

To be explicitly clear, it's important to check the result any time the query may have failed. Which you don't know... hence always.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜