duandiao3961 2013-01-30 01:02
浏览 18
已采纳

php mysql获取带有字符问题的名称

Hi i have a list of restaurants name in my db where some of the name comes with character like &, @, and ' (quote), the way the name are displayed in browser when viewing then are http://localhost/my-restaurant-new-york as i use this function to replace empty spaces with dash -

$businessDetail = strtr($businessDetail, '-', ' ');

based on business name an business id will be found and retrieve all the related infos. If in my db i have a name like My Restaurant New & york i cause an error in sql as follow

Message: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 3.

The question now is how can i save the name in the beginning and how to retrieve it back without having an issue with special characters. Thanks

UPDATE: i am using zend framework so this is how i save name into db and retrieve back

$testMapper = new Application_Model_Mapper_TestMapper();
$testModel = new Application_Model_Test();

$bzname =  str_replace("'", '', $this->_getParam('name'));
$testModel->setId($id)
          ->setName($bzname);
$business_id = $testMapper->save($testModel);

All link to the business name are translated by this function

$this->view->bzurl = preg_replace("![^a-z0-9]+!i","-", $result['business_name']);

Update2:

public function getBusinessId($business_detail)
    {
        $select = $this->getDbTable()->getAdapter()->select();
      $select->from('business',array('business_id'))

               ->where("business_name='".$business_detail."'"); 



        $result = $this->getDbTable()->getAdapter()->fetchRow($select);
        return $result['business_id'];      
    }
  • 写回答

1条回答 默认 最新

  • dongyin2390 2013-01-30 01:30
    关注
    ->where("business_name='".$business_detail."'")
    

    should be:

    ->where("business_name = ?", $business_detail)
    

    to ensure that the data is correctly escaped. If that's the query generating the error, that should fix your issue. I'd recommend you read up a little on SQL injection and how to avoid it.

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

报告相同问题?

悬赏问题

  • ¥15 求一下解题思路,完全不懂
  • ¥15 tensorflow
  • ¥15 densenet网络结构中,特征以cat方式复用后是怎么进行误差回传的
  • ¥15 STM32G471芯片spi设置了8位,总是发送16位
  • ¥15 R语言并行计算beta-NTI中tree文件的类型
  • ¥15 如何解读marsbar导出的ROI数据?
  • ¥20 求友友协助弄一下基于STC89C52单片机的声光控制灯原理图
  • ¥15 arduino双向交通灯设计
  • ¥15 有没有会粒子群算法的大能(○゜ε^○)求带不会出收敛图😭
  • ¥15 Matlab读取根元素出错