PHP PDO 获取 null

发布于 2024-09-02 03:16:06 字数 642 浏览 5 评论 0原文

如何检查列值是否为空?示例代码:

$db = DBCxn::getCxn();

$sql = "SELECT exercise_id, author_id, submission, result, submission_time, total_rating_votes, total_rating_values
FROM submissions 
LEFT OUTER JOIN submission_ratings ON submissions.exercise_id=submission_ratings.exercise_id
WHERE id=:id";

$st = $db->prepare($sql);

$st->bindParam(":id", $this->id, PDO::PARAM_INT);

$st->execute();
$row = $st->fetch();

$this->total_rating_votes = $row['total_rating_votes'];

if($this->total_rating_votes == null) // this doesn't seem to work even though there is no record in submission_ratings????
{
...
}

How do you check if a columns value is null? Example code:

$db = DBCxn::getCxn();

$sql = "SELECT exercise_id, author_id, submission, result, submission_time, total_rating_votes, total_rating_values
FROM submissions 
LEFT OUTER JOIN submission_ratings ON submissions.exercise_id=submission_ratings.exercise_id
WHERE id=:id";

$st = $db->prepare($sql);

$st->bindParam(":id", $this->id, PDO::PARAM_INT);

$st->execute();
$row = $st->fetch();

$this->total_rating_votes = $row['total_rating_votes'];

if($this->total_rating_votes == null) // this doesn't seem to work even though there is no record in submission_ratings????
{
...
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

久而酒知 2024-09-09 03:16:06

连接到数据库时,可以设置一些属性来控制 PDO 在数据库查询返回 Null 和空字符串时如何处理它们

PDO::setAttribute (PDO::ATTR_ORACLE_NULLS, $option )

其中 $option 是以下之一:

  • PDO::NULL_NATURAL:无转换。
  • PDO::NULL_EMPTY_STRING:空字符串转换为 NULL。
  • PDO::NULL_TO_STRING:NULL 转换为空字符串。

When you connect to the database, you can set some attributes to control how PDO handles Nulls and Empty Strings when they are returned by the database query

PDO::setAttribute (PDO::ATTR_ORACLE_NULLS, $option )

Where $option is one of the following:

  • PDO::NULL_NATURAL: No conversion.
  • PDO::NULL_EMPTY_STRING: Empty stringis converted to NULL.
  • PDO::NULL_TO_STRING: NULL is converted to an empty string.
始终不够 2024-09-09 03:16:06

这难道不是你想做的事吗?

foreach($row as $r){

if($r->total_rating_votes == null){

  //do something

}

实际上你可能想尝试:

if($r->total_rating_votes == ""){/*do something*/}

因为 php 可能已经将 null 值转换为空字符串,然后它实际上不是 null,而是 ""

希望这有帮助!

Isnt it something like that that you want to do?

foreach($row as $r){

if($r->total_rating_votes == null){

  //do something

}

Actually you might want to try:

if($r->total_rating_votes == ""){/*do something*/}

Because php might have converted the null value into an empty string, and then it's not actually null, it's ""

Hope this helps!

傲性难收 2024-09-09 03:16:06

感谢您的所有回答。经过一番实验后,这段代码解决了我的问题

$this->total_rating_votes = $row['total_rating_votes'];

if(!isset($this->total_rating_votes)) // this is now true if this record had a NULL value in the DB!!!
{
...
}

Thanks for all of your answers. After a bit of experimentation this code solved my problem

$this->total_rating_votes = $row['total_rating_votes'];

if(!isset($this->total_rating_votes)) // this is now true if this record had a NULL value in the DB!!!
{
...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文