doutuo3575 2014-11-13 01:01
浏览 46
已采纳

空MySQL数据库表格单元格触发“内容”切换

I'm having a really weird problem with my MySQL database. To make sure I wasn't make a dumb mistake, I tested it without the database...

$Content = 'Hello World';
// $Content = '';
switch ($Content)
{
 case '':
 echo 'No Content 1';
 break;
 default:
 echo 'Content 1';
 echo $Content;
 echo '<br><br>';
 break;
}

It works. When content equals 'Hello World' it echoes "Content 1." When I set it to $Content = '', it echoes "No Content 1."

But when I delete the first two lines and insert "Hello World" in my database, something funny happens.

Actually, it works correctly at first. But when I delete the content from the database, it still displays "Content 1," even though there's no database content. (It doesn't echo the value for $Content.)

I checked the database to see if there might be a 0 (zero) in the cell, but there isn't. There isn't even a simple space.

On a whim, I added the value 0 to my switch:

switch ($Content)
{
 case '':
 case 0:
 echo 'No Content I';
 break;
 default:
 echo 'Content I<br><br>';
 echo $Content;
 break;
}

Now it does just the opposite, display "No Content" whether there's content in the database or not.

Am I making some simple mistake? I upgraded MAMP a couple days ago, so I wondered if it might be some kind of software bug.

ON EDIT: I Googled for more information and found this solution...

if ($Content != 0) {
 echo "true";
 echo $Content;
} else {
echo "false";
echo $Content;
}

However, it doesn't work for me, either; it returns "false" where there's content in the database or not.

  • 写回答

1条回答 默认 最新

  • douzhao1912 2014-11-13 02:03
    关注

    switch uses loose comparison, which means that non-empty strings are interpreted as 0 when comparing with an integer, unless the initial portion of the string converts to a non-zero number according to the rules in String conversion to numbers.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器