您好我正在使用mysqli来替换表中的默认值,如果数据库中的数据为NULL。我已经在PHPmyAdmin上尝试了它并且它正在工作但不在我的代码上:(
这是我的SELECT查询:
$query="SELECT pro_id, pro_name, unit_name, cat_name, IFNULL(quantity,'empty') AS quantity FROM products, unit, categories WHERE products.unit=unit.unit_id AND products.pro_cat=categories.cat_id";
答案 0 :(得分:2)
如果您的评论之一似乎表明,您收到的错误是:
Incorrect parameter count in the call to native function 'ISNULL'
那么这只是一个简单的拼写错误。 ISNULL
与IFNULL
不同。
如果一个参数为空,前者返回一个真值。
如果第一个参数为null,则后者返回第二个参数,否则返回第一个参数。
如果将以下代码放入SqlFiddle:
,可以看到这一点-- DDL
create table xyzzy (plugh int);
insert into xyzzy (plugh) values (null);
insert into xyzzy (plugh) values (42);
select plugh, isnull(plugh) from xyzzy;
select plugh, ifnull(plugh,-1) from xyzzy;
select plugh, isnull(plugh,-1) from xyzzy;
输出与前两个select语句一样,而第三个产生您描述的错误:
plugh isnull(plugh)
------ -------------
(null) 1
42 0
plugh ifnull(plugh,-1)
------ ----------------
(null) -1
42 42
Incorrect parameter count in the call to native function 'isnull'