抱歉,这可能是一个非常愚蠢的问题,但是在人们将要查看的页面上运行此代码是否安全,或者我应该将其包装到函数中并调用它?
$stmt = $db->prep_stmt("select * from .... where userid = ? and username = ?");
/* Binding 2 parameters. */
$stmt->bind_param("is", $userid, $username);
/* Binding 2 result. */
$stmt->bind_result($isbn, $title, $author, $coef, $bookid);
/* Executing the statement */
$stmt->execute( ) or die ("Could not execute statement");
/*
* Making PHP buffer the whole result,
* not recommended if there is a blob or
* text field as PHP eats loads of memory
*/
$stmt->store_result();
while ($stmt->fetch()) {
/*
* Here you can use the variables $isbn, $title, $author, $coef, $bookid,
* which contatin the data for 1 row.
*/
print "<tr>".
"<td>".$isbn."</td>".
"<td>".$title."</td>".
"<td>".$author."</td>".
"</tr><tr><td>";
}
最佳答案
从安全角度来看,它们是相同的。这是软件设计的问题。但是,您可能需要考虑更好的错误处理(至少对于生产而言)。具体来说,实际上没有必要泄漏错误的原因(“无法执行语句”)。通常,您需要一个通用错误页面(“抱歉,服务器有问题!尝试转到主页。”)。
关于php - 在人们将访问的页面上运行此查询是否安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4467635/