doucai9270 2014-04-30 09:38
浏览 160
已采纳

如何为字符串设置默认值?

I have a page that imports fields from a csv file and imports them into my database. Before the fields can be imported into my database I need to separate the name field into two separate values (first name & last name). I do it like so:

$name = $order[5];
$fname = sqlSafe(trim(substr($name, 0, strrpos($name, " "))));`
$lname = sqlSafe(trim(substr($name, strrpos($name, " "))));`

This seems to work reasonably well, but sometimes the name field has been left partially complete (they've entered just their first name or last name for example). This results $fname being blank and mysql won't allow me to set a default value for a BLOB/TEXT column.

To solve this I have tried a couple of if statements including:

if (!$fname) {$fname = "(blank)";}
if ($fname = "") {$fname = "(blank)";}
if ($fname = NULL) {$fname = "(blank)";}

None of which seem to catch it. Where am I going wrong?

  • 写回答

5条回答 默认 最新

  • dongzhi4470 2014-04-30 09:40
    关注

    The single = in your ifs will actually not test but assign a value. Use ==or better === to test with if.

    More info on test operators: http://www.php.net/manual/en/language.operators.comparison.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 请提供一个符合要求的网页链接。
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码