2014-02-23 64 views
0

這對你們中的一些人來說可能是一塊蛋糕,但對我來說這是一個挑戰。 我正在努力考慮安全性問題,並且因爲我是PHP和Mysql的新手,所以我絕對不能100%確定我的工作。那麼這個代碼中的任何一個點都會出現錯誤?這個代碼中有沒有人能指出錯誤?準備好的聲明

$sql = 'SELECT caracteristique_id, caracteristique, obligatoire FROM caracteristique WHERE categorie = ?'; 


if(mysqli_prepare($con, $sql)){ 
    $stmt = mysqli_prepare($con, $sql); 
    mysqli_stmt_bind_param($stmt, 's', $cat); 
    mysqli_stmt_execute($stmt); 
    $row = array(); 
    mysqli_stmt_bind_result($stmt, $row['caracteristique_id'], $row['caracteristique'], $row['obligatoire']); 

while(mysqli_stmt_fetch($stmt)){ 
// do stuff here 
} 
} 

任何幫助將會和將會明白。 N.B.我在網上做了很多研究,我只是不確定100%。

+0

請出示表'caracteristique'的佈局。 –

+0

其實它包含很多列,但爲了簡潔起見,我只提到了兩個。沒有什麼特別的關於其他cols – user3134422

+0

你提到三個和'caracteristique'突出,因爲它也是表名。 –

回答

0

可以(優化/改寫)的代碼段(這將是更ORMised):

$sql = 'SELECT caracteristique_id, caracteristique, obligatoire FROM caracteristique WHERE categorie = ?'; 
$stmt = mysqli_prepare($con, $sql); 
if($stmt) { 
    $stmt->bind_param('s', $cat); 
    $stmt->execute(); 
    $row = array(); 
    $stmt->bind_result($row['caracteristique_id'], $row['caracteristique'], $row['obligatoire']); 
    while($stmt->fetch()) { 
     // do stuff here 
    } 
} 

其他,我不認爲有與該段在所有的任何問題。它應該工作正常。


至於錯誤等,把上面的段內try-catch塊:

try { 
    /* The segment from above */ 
} catch (mysqli_sql_exception $e) { 
    echo $e->errorMessage(); 
} 
+0

謝謝,但什麼是ORMised?還有,我猜這是OOP風格,不是嗎? – user3134422

+0

除了如何提示異常或錯誤? – user3134422

+0

@ user3134422 [ORM](https://en.wikipedia.org/wiki/Object-relational_mapping)。至於錯誤,把整個段放在'try-catch'塊中。 – hjpotter92