I am using the WPDB object inside of Wordpress to communicate with a MySQL database. My database has a column with a type of bit(1), however, Wordpress does not extract these as a 0 or 1 on my production server (they did on my local machine).

我正在使用Wordpress中的WPDB对象与MySQL数据库进行通信。我的数据库有一个类型为bit(1)的列,但是Wordpress在我的生产服务器上没有将它们提取为0或1(它们在我的本地机器上)。


Question:

问题:

If I have a database value from Wordpress, I can't do a simple comparison to 0 or 1:

如果我有一个Wordpress的数据库值,我不能做一个简单的比较0或1:

if ($data[0]->Sold == 1) { //Always false
...
if ($data[0]->Sold == 0) { //Always false

How can I check if the value is 0 of 1?

如何检查值是否为0 (1)?


Background:

背景:

This was not an issue on my local machine, but only in production.

这不是我本地机器上的问题,而是生产上的问题。

I query the database like this:

我这样查询数据库:

$data = $wpdb->get_results("...");

When I do a var_dump() on the results from the database, here is the output my browser shows:

当我对来自数据库的结果执行var_dump()时,下面是我的浏览器显示的输出:

array(1) {
  [0] => object(stdClass)#261 (10) {
    ["SaleID"]     => string(4) "1561"
    ["BookID"]     => string(2) "45"
    ["MerchantID"] => string(1) "1"
    ["Upload"]     => string(19) "2012-11-20 15:46:15"
    ["Sold"]       => string(1) ""
    ["Price"]      => string(1) "5"
    ["Condition"]  => string(1) "5"
    ["Written"]    => string(1) ""
    ["Comments"]   => string(179) "<p>I am the first owner of this barely used book. There aren't any signs of normal wear and tear, not even any creases on the cover or any of its pages. It would pass for new.</p>"
    ["Expiring"]   => string(19) "2013-05-20 15:46:15"
  }
}

Notice how Sold and Written show a string size of 1, but don't have an associated value. These values should be populated with 1 and 0, respectively.

注意如何销售和写入显示字符串大小为1,但没有关联值。这些值应该分别填充为1和0。

The Chrome inspector tool shows something quite interesting for these values:

对于这些值,Chrome inspector工具显示了一些非常有趣的东西:

What is \u1 or \u0 and why aren't they simply 1 or 0, so I can do comparisons?

什么是\u1或\u0 ?为什么它们不是简单的1或0 ?

Thank you for your time.

谢谢您的时间。

2 个解决方案

#1


1

Check this answer out: https://stackoverflow.com/a/5323169/794897

检查这个答案:https://stackoverflow.com/a/5323169/794897

"When you select data from a MySQL database using PHP the datatype will always be converted to a string."

“当您使用PHP从MySQL数据库中选择数据时,数据类型将始终转换为字符串。”

You can either do:

你可以做的:

if ($data[0]->Sold === "1") { 
...
if ($data[0]->Sold === "0") { 

or type cast the variable, e.g.

或类型转换变量,例如。

$Sold = (int) $data[0]->Sold;

if ($Sold === 1) { 
...
if ($Sold === 0) { 

更多相关文章

  1. c#操作mysql事务是不是要在一个数据库连接内完成?
  2. 如何在MySQL数据库和JPA中使用Spring Boot?
  3. 无法连接远程MySQL数据库的解决方案
  4. MySQL 示例数据库sakila
  5. 在H2数据库中插入时间——函数“PARSEDATETIME”未找到
  6. 由于底层异常,无法加载连接类:'java.lang.NumberFormatException:对
  7. 使用mysql和PHP从本地主机连接到远程数据库
  8. 关于MySQL数据库操作
  9. labview使用DSN与数据库的连接包括access,mysql

随机推荐

  1. 校招录取之后前往公司实习,现在感觉自己前
  2. 如何将dict转换为spark map输出
  3. 当使用一个传送到另一个的python文件时,我
  4. matplotlib绘制符合论文要求的图片
  5. classmethod,staticmethod,还有类里面一般
  6. Python文件操作大全,随机删除文件夹内的任
  7. 获取错误“ValueError:int()的无效文字,基数
  8. C++各大有名库的介绍——准标准库Boost
  9. 从Python中的列表元素中删除URL
  10. python传递列表作为函数参数