有没有办法在 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/