我有一个登录系统,我一直在使用 mysql 函数,如 real_escape_strings 等。但现在我正试图将其转换为 PDO,因为它比 mysql 函数更现代。问题是现在它不适用于 PDO。下面是我的代码,请告诉我我做错了什么。
<?php
try {
$con = new PDO('mysql:host=localhost;dbname=tish_database;charset=utf8','root','');
} catch(PDOException $e){
echo 'Connection failed'.$e->getMessage();
}
?>
<?php
$is_ajax = $_POST['is_ajax'];
if(isset($is_ajax) && $is_ajax)
{
$username =(isset($_POST['username']))? trim($_POST['username']): '';
$Password=(isset($_POST['Password']))? $_POST['Password'] : '';
$redirect=(isset($_REQUEST['redirect']))? $_REQUEST['redirect'] :
'view.php';
$query ='SELECT username FROM tish_user WHERE '.
'username="'.($username,$con).'" AND ' .
'Password = md5("'.($Password,$con).'")';
$result = $con->prepare($query);
$result->execute();
if(count($result)>0)
{
$_SESSION['username']=$username;
$_SESSION['logged'] = 1;
echo "success";
}
else {
//set these explicitly just to make sure
}
}
?>