嗨,我的数据库中有一个餐馆名称列表,其中一些名称带有 &、@ 和 '(引号)
等字符,这是查看时名称在浏览器中显示的方式然后是 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'];
}
最佳答案
->where("business_name='".$business_detail."'")
应该是:
->where("business_name = ?", $business_detail)
确保数据正确转义。如果这是生成错误的查询,那么应该可以解决您的问题。我建议您阅读一些有关 SQL 注入(inject)以及如何避免它的内容。
关于php mysql 获取带有字符问题的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14595003/