php和mysql函数的区别吗,(PHP,MySQL)函数仅在2种情况中的1种有效,找不到区别
因此,我具有此功能來搜索MySQL數據庫中的條目:
private function SearchContributors($search)
{
$search_pieces = explode(' ', $search);
if (count($search_pieces) == 1 )
{
$this->db->like('firstname', $search);
$this->db->or_like('lastname', $search);
$result = $this->db->get(); //the line from the error message below
}
//Other stuff for 2 and more pieces
return $result;
}
?>
我有兩次使用該功能.
案例A是用戶發起的搜索,并從URL(domain.com/contributors/?x=paul)獲取搜索查詢.這很好.
if (isset($_GET['x']))
{
$x = $_GET['x'];
$result = $this->SearchContributors($x);
}
?>
情況B是用戶輸入無效的子彈名稱(domain.com/contributors/paul代替domain.com/contributors/pauline-surname)并直接獲取搜索查詢時的備份:
$this->db->where('slug', $slug);
$result = $this->db->get();
if ($result->num_rows() == 0)
{
$x = str_replace('-', ' ', $slug);
$result = $this->SearchContributors($x);
}
?>
這返回了MySQL語法錯誤:
Error Number: 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 ‘WHERE firstname LIKE ‘%paul%’ OR lastname LIKE ‘%paul%” at line 2
SELECT * WHERE firstname LIKE ‘%paul%’ OR lastname LIKE ‘%paul%’
Filename: /www/htdocs/w00a94ee/c/controllers/contributors.php
Line Number: 23
在這兩種情況下,該函數都具有完全相同的字符串保羅,那么為什么它不起作用?
//編輯
function __construct()
{
parent::__construct();
$this->load->database('databasename');
$this->db->from('tablename');
}
解決方法:
您忘記了指定要從中選擇哪個表.
$this->db->from('tablename');
編輯:問題是您在構造函數中添加from,然后您調用:
$this->db->where('slug', $slug);
$result = $this->db->get();
在致電SearchContributors之前.這將運行查詢并重置變量.
因此,當您調用SearchContributors時,將不再設置FROM.
您需要將$this-> db-> from(‘tablename’);在SearchContributors而不是構造函數中.使模型函數自成一體,而不需要外部函數(例如__construct來調用它們)通常是一個好主意.
標簽:codeigniter,mysql,php
來源: https://codeday.me/bug/20191127/2077035.html
總結
以上是生活随笔為你收集整理的php和mysql函数的区别吗,(PHP,MySQL)函数仅在2种情况中的1种有效,找不到区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java注解@remote,Dwr3.0
- 下一篇: php的pathinfo,php中Pat