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://*.com/a/5323169/794897
检查这个答案:https://*.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) {
#2
0
For me the solution was to use ord function: http://us1.php.net/manual/en/function.ord.php
对我来说,解决方案是使用ord函数:http://us1.php.net/manual/en/function.ord.php
Edit
编辑
Yet the behavior seems to differ depending on the server. On Arch Linux with MariaDB 10.0.14 and Ubuntu with MySQL 5.5.37-0ubuntu0.13.10.1 wpdb returns good old "0" or "1" strings, not the problematic bit-strings, which happen on CentOS 6.4 MySQL 5.1.73
然而,根据服务器的不同,这种行为似乎有所不同。在使用MariaDB 10.0.14和Ubuntu的Arch Linux上,使用MySQL 5.5.37-0ubuntu0.13.10.1 . wpdb返回好的旧的“0”或“1”字符串,而不是在CentOS 6.4 MySQL 5.1.73上发生的问题的位串。
#1
1
Check this answer out: https://*.com/a/5323169/794897
检查这个答案:https://*.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) {
#2
0
For me the solution was to use ord function: http://us1.php.net/manual/en/function.ord.php
对我来说,解决方案是使用ord函数:http://us1.php.net/manual/en/function.ord.php
Edit
编辑
Yet the behavior seems to differ depending on the server. On Arch Linux with MariaDB 10.0.14 and Ubuntu with MySQL 5.5.37-0ubuntu0.13.10.1 wpdb returns good old "0" or "1" strings, not the problematic bit-strings, which happen on CentOS 6.4 MySQL 5.1.73
然而,根据服务器的不同,这种行为似乎有所不同。在使用MariaDB 10.0.14和Ubuntu的Arch Linux上,使用MySQL 5.5.37-0ubuntu0.13.10.1 . wpdb返回好的旧的“0”或“1”字符串,而不是在CentOS 6.4 MySQL 5.1.73上发生的问题的位串。