簡體   English   中英

cakephp 2.6從數據庫檢索數據

[英]cakephp 2.6 retrieving data from database

這是我在cakephp控制器中編輯的函數:

public function edit($id = null) { 
if ($this->request->is('get')) {
            $this->request->data = $this->Topic->findById($id);
        }
...

第一個問題是傳遞給函數的參數id是字符串類型而不是整數類型。 其次,在數據庫中有一個id為14的主題,這兩個代碼都檢索相同的結果,我不明白為什么:

http://localhost/cakephp1/topics/edit/14
http://localhost/cakephp1/topics/edit/14anyCharactersHere

URL傳遞的參數當然是字符串類型的,因為URL是字符串。

假設您數據庫中的id列為整數,那么Cake將把傳遞給findById()的參數轉換為整數: (int)"14"(int)"14anyCharactersHere"(int)"14anyCharactersHere" 14 因此,問題中的兩個URL都將導致從數據庫中提取相同的數據。

如果要確保$id僅包含一個整數,請使用ctype_digit()

if (ctype_digit($id) === true) {
    // Is integer
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM