本文介绍了IFNULL不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,如果数据库中的数据为NULL,我正在与mysqli一起替换表上的默认值.我已经在PHPmyAdmin上尝试过了,并且可以正常工作,但是在我的代码上却不行:(

Hi I am working with mysqli to replace a default value on the table if the data from the database is NULL. I already tried it on PHPmyAdmin and it's working but not on my code :(

这是我的SELECT查询:

Here's my SELECT query:

$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";

推荐答案

如您的评论之一所示,如果得到的错误是:

If, as one of your comments seems to indicate, the error you're getting is:

Incorrect parameter count in the call to native function 'ISNULL'

那么这是一个简单的错字. ISNULL IFNULL .

then it's a simple typo. ISNULL is not the same as IFNULL.

如果前者的 one 参数为null,则前者将返回真值.

The former returns a truth value if its one argument is null.

如果第一个为null,则后者返回第二个参数,否则返回第一个参数.

The latter returns the second argument if the first is null, otherwise it returns the first argument.

如果将以下代码放入SqlFiddle中,您将看到此消息:

You can see this if you put the following code into 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;

前两个选择语句的输出与预期的一样,而第三个选择语句则产生您所描述的错误:

The output is as expected for the first two select statements while the third generates the error you describe:

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'

这篇关于IFNULL不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 11:20