douweng1904 2014-04-30 02:06
浏览 91
已采纳

选择或显示NULL值错误

I have a

$dummy = 1  
$dummy = 2
$dummy = null

and a query and tried display it

$query = "select * from table1 where sy=2000 AND sy_dummy=$dummy";
echo $query;

then when $dummy=null, I get

ERROR: syntax error at or near "order" LINE 2: AND sy_dummy= order by table1 ^

and i tried $dummy IS NULL but gives me blank page.

My problem is that how can I put a NULL value to my $dummy and gives me something like the result should be .....sy_dummy IS NULL order by table1

not ......sy_dummy= order by table1;

Is this possible? Hope you understand my problem.

  • 写回答

2条回答 默认 最新

  • dongpanshi2839 2014-04-30 02:14
    关注

    I would check for the null in PHP and modify how you're forming your query string in your php logic.

    This allows you to prepare the statement variables (safe and secure) rather than simply entering them inline into the query (potentially dangerous and open to SQL Injection)

    if(!isset($dummy) || $dummy === null)
    {
      // Form your $query here using sy_dummy IS NULL
    }
    else
    {
      // Form your $query here using sy_dummy =, and bind your variables
    }
    

    Also take a look into PDO and Prepare Statements for writing safe, secure database interactions.

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

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line