嗨,我的数据库中有一个餐厅名称列表,其中一些名称带有类似字符 &, @, and ' (quote)
,查看时名称在浏览器中的显示方式是http://localhost/my-restaurant-new-york
因为我使用此功能用破折号替换空格-
$businessDetail = strtr($businessDetail, '-', ' ');
根据企业名称,将找到企业 ID 并检索所有相关信息。如果在我的数据库中我有一个像My Restaurant New & york
我这样的名字会导致 sql 错误如下
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.
现在的问题是如何在开头保存名称以及如何将其取回而不会出现特殊字符问题。谢谢
更新:我正在使用 zend 框架,所以这就是我将名称保存到数据库并取回的方式
$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);
商家名称的所有链接都由此功能翻译
$this->view->bzurl = preg_replace("![^a-z0-9]+!i","-", $result['business_name']);
更新2:
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'];
}