Oli*_*ryn 7 php mysql sql database wordpress
我正在使用Wordpress内部的WPDB对象与MySQL数据库进行通信.我的数据库有一个类型的列bit(1),但是,Wordpress不会将它们作为一个0或1我的生产服务器(它们在我的本地计算机上)提取.
题:
如果我有一个来自Wordpress的数据库值,我无法与0或进行简单的比较1:
if ($data[0]->Sold == 1) { //Always false
...
if ($data[0]->Sold == 0) { //Always false
Run Code Online (Sandbox Code Playgroud)
如何检查值是否0为1?
背景:
这不是我本地机器上的问题,而只是在生产中.
我像这样查询数据库:
$data = $wpdb->get_results("...");
Run Code Online (Sandbox Code Playgroud)
当我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"
}
}
Run Code Online (Sandbox Code Playgroud)
请注意如何Sold与Written显示的字符串大小1,但不具有关联的值.这些值应分别用1和填充0.
Chrome检查器工具会显示这些值非常有趣的内容:


什么是\u1或者\u0为什么不是他们根本1或0,所以我可以做比较?
感谢您的时间.
查看这个答案: https ://stackoverflow.com/a/5323169/794897
“当您使用 PHP 从 MySQL 数据库中选择数据时,数据类型将始终转换为字符串。”
你可以这样做:
if ($data[0]->Sold === "1") {
...
if ($data[0]->Sold === "0") {
Run Code Online (Sandbox Code Playgroud)
或类型转换变量,例如
$Sold = (int) $data[0]->Sold;
if ($Sold === 1) {
...
if ($Sold === 0) {
Run Code Online (Sandbox Code Playgroud)