Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Wordpress数据库类-MySQL类型位_Php_Mysql_Sql_Database_Wordpress - Fatal编程技术网

Php Wordpress数据库类-MySQL类型位

Php Wordpress数据库类-MySQL类型位,php,mysql,sql,database,wordpress,Php,Mysql,Sql,Database,Wordpress,我使用Wordpress内部的WPDB对象与MySQL数据库通信。我的数据库有一个类型为bit1的列,但是Wordpress不会在我的生产服务器上将其提取为0或1,而在我的本地计算机上则是这样 问题: 如果我有Wordpress的数据库值,我无法与0或1进行简单比较: 如何检查该值是否为1的0 背景: 这不是我本地机器上的问题,只是在生产中 我按如下方式查询数据库: $data = $wpdb->get_results("..."); 当我对来自数据库的结果执行var_转储时,我的浏览器

我使用Wordpress内部的WPDB对象与MySQL数据库通信。我的数据库有一个类型为bit1的列,但是Wordpress不会在我的生产服务器上将其提取为0或1,而在我的本地计算机上则是这样

问题:

如果我有Wordpress的数据库值,我无法与0或1进行简单比较:

如何检查该值是否为1的0

背景:

这不是我本地机器上的问题,只是在生产中

我按如下方式查询数据库:

$data = $wpdb->get_results("...");
当我对来自数据库的结果执行var_转储时,我的浏览器显示以下输出:

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"
  }
}
请注意,Sald和Write显示的字符串大小是1,但没有关联的值。这些值应分别填充为1和0

Chrome inspector工具为这些值显示了一些非常有趣的内容:

什么是\u1或\u0,为什么它们不是简单的1或0,这样我可以进行比较

谢谢您的时间。

请查看以下答案:

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

您可以执行以下任一操作:

if ($data[0]->Sold === "1") { 
...
if ($data[0]->Sold === "0") { 
或键入强制转换变量,例如

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

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

对我来说,解决方案是使用ord函数:

编辑


然而,行为似乎因服务器而异。在带有MariaDB 10.0.14的Arch Linux和带有MySQL 5.5.37-0ubuntu0.13.10.1的Ubuntu上,wpdb返回的是旧的0或1字符串,而不是问题位字符串,这发生在CentOS 6.4 MySQL 5.1.73上,听起来好像MySQL数据库中没有设置这些值。请尝试更新mySQL中的行。@fredrik我提到过:这些值应该分别用1和0填充。很遗憾,u1和u0是内容,而控制字符的unicode表示形式是内容。也许你可以试着把它们转换成一个整数,然后我猜你已经通过编程添加了一个1或0字符,而不是一个包含1或0的字符串。这将解释\u0和\u1。意思是:您在行中放置了0或1,而不是0或1。很大的不同。@fredrik,这真的可能是原因。[at]spryno724另请参见MySQL文档:
$Sold = (int) $data[0]->Sold;

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