php - How to change those MySQL codes to PDO ? and how to escape a string with pdo codes? -
$sql = "insert users(user_name, user_pass, user_email, user_date, user_level) values('" . mysql_real_escape_string($_post['user_name']) . "' , '" . sha1($_post['user_pass']) . "' , '" . mysql_real_escape_string($_post['user_email']) . "' , now(), 0)"; $result = mysql_query($sql);
// configuration $dbtype = "sqlite"; $dbhost = "localhost"; $dbname = "test"; $dbuser = "root"; $dbpass = "admin"; // database connection $conn = new pdo("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass); $user_name='mohoni'; $user_pass='mohinipass'; $user_email='mohini@yopmail.com'; $user_date='2014-03-21'; $user_level='first'; // query $sql = "insert users (user_name,user_pass,user_email,user_date,user_level) values (:user_name,:user_pass,:user_email,:user_date,:user_level)"; $q = $conn->prepare($sql); $q->execute(array(':user_name'=>$user_name, ':user_pass'=>$user_pass, ':user_email'=>$user_email, ':user_date'=>$user_date, ':user_level'=>$user_level));
you can use $conn->quote
string follows :
$conn->quote($user_name);
Comments
Post a Comment