dongqiaozhe5070 2014-04-17 13:36
浏览 49
已采纳

尝试使用字母时从数据库中选择时未捕获的异常'ErrorException'

I'm trying to select a value from the database, it works perfectly if everything is just numbers - but fails to work if there are letters involved. This is in OpenCart.

My column is called campaign_code , its type is VARCHAR.

Here is the command...

$campaign_id = $this->db->query("SELECT campaign_id FROM " . DB_PREFIX . "campaign WHERE campaign_code = ". $this->db->escape($campaign_code)); 

When $campaign_code equals any number it's fine. It selects as expected.

When its letters or a combination of numbers or letters I get this error...

Uncaught exception 'ErrorException' with message 'Error: Unknown column '85f' in 'where clause'<br />Error No: 1054<br />SELECT campaign_id FROM oc_campaign WHERE campaign_code = 85f' in C:\xampp\htdocs\microcharity\system\database\mysqli.php:40

Thank you.

  • 写回答

2条回答 默认 最新

  • doujiu8178 2014-04-17 13:58
    关注

    While using an integer in where clause, you can avoid quotes but while using a string you have to enclose it in single quotes.

    For integer -

    $campaign_id = $this->db->query("SELECT campaign_id FROM '".DB_PREFIX."' WHERE campaign_code = ".$this->db->escape($campaign_code).""); 
    


    For string -

    $campaign_id = $this->db->query("SELECT campaign_id FROM '".DB_PREFIX."' WHERE campaign_code = '".$this->db->escape($campaign_code)."'"); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图