php - PHP 中的 MySQL NULL

标签 php mysql null quotes

有没有办法在 mysql 语句中“动态地”放置一个 php 字符串变量,以便如果它的值为“NULL”,则在查询语句中删除引号?这是代码:

$sql="UPDATE products SET title='$title', supplier='$supplier', availability='$availability', condition='$condition', brand='$brand', power='$power', category='$category', min_qty='$min_qty', cost='$cost' WHERE id='$id'";

所有变量都是字符串,但是我希望如果这些字符串之一是“NULL”,它会被有效地视为 mysql NULL(而不是“NULL”字符串)。有没有快速的方法来做到这一点?提前致谢!

最佳答案

你可以这样做:

$sql="UPDATE products SET title='$title', supplier='$supplier', availability='$availability', condition=".($condition != null) ? "(".$condition.")" : "NULL"", brand='$brand', power='$power', category='$category', min_qty='$min_qty', cost='$cost' WHERE id='$id'";

查看 $condition。这是在字符串中使用 if 语句的简单方法。

关于php - PHP 中的 MySQL NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25569166/

相关文章:

sql - 如果行为 NULL,则获取上一行的值

Java初始化变量错误

c++ - 如何检查指针是否指向 NULL?

php - Exec() w/-rwxr-xr-x 的权限被拒绝

mysql - SQL View 显示 12 个月和按年的间隔

PHP - 无法修改 header 信息 - header 已发送(输出开始于

mysql - Zend_Db查询Mysqli准备错误: Operand should contain 1 column(s)

java - Mysql 没有重新连接 JNDI Tomcat 6

php - 一条sql语句比两条sql语句好吗?

php - Laravel try/catch 不工作