【发布时间】:2013-01-26 08:48:17
【问题描述】:
我有几个问题。
规范:MySql 数据库;服务器端语言 PHP 5.3.10
1) 什么时候应该使用准备好的语句?
我正在构建一个有用户的 web 应用程序。我不断地在数据库中检索/插入数据。我目前没有使用准备好的语句,我想知道这是否是错误的做事方式?
/**
* Register a new user into the database. Please make sure to
* hash the password.
* @param type $fName First name of the user - String value.
* @param type $lName Last name of the user - String value.
* @param type $email Email address of the user - String value.
* @param type $hashedPassword - String value.
* @return boolean true if sucessful or false if failed.
*/
function registerUser($fName, $lName, $email, $hashedPassword)
{
//Establish a connection.
$mysqli = new $mysqli($GLOBALS['dbServer'], $GLOBALS['dbUserName'], $GLOBALS['dbPassword'], $GLOBALS['dbName']);
//Check if connection failed.
if($mysqli->connect_error)
{
die('Connect Error (' .$mysqli->connect_errno . ') '
.$mysqli->connect_error);
}
//Insert data into the table and check if the attempt was sucessful.
if($mysqli->query("INSERT INTO user_info(email, password, fName, lName) VALUE ('$email', '$hashedPassword', '$fName', '$lName')"))
{
return true;
}
return false;
}
这是将值插入数据库并确保它成功的正确方法吗?或者,我可以使用准备好的语句,我想知道
2) 我将如何使用准备好的语句?我为什么要(如果你建议我这样做)?
我预计该网站每天的访问量约为 20,000 次。或者让我们假设有多少......
【问题讨论】:
-
没有冒犯,但这个问题(“我为什么要”)已经被问过数千次了。为什么不先搜索一下? for exapmle
标签: php mysql mysqli prepared-statement web-traffic