dtef9322 2012-02-03 09:30
浏览 75
已采纳

将mysql转换为PDO语句

This is the login function written using MySQL way However, the problem exists when it convert into PDO way

MYSQL:

    <?
function confirmUser($username, $password){
   global $conn;
   if(!get_magic_quotes_gpc()) {
    $username = addslashes($username);
   }

   /* Verify that user is in database */
   $q = "select UserID,UserPW from user where UserID  = '$username'";
   $result = mysql_query($q,$conn);
   if(!$result || (mysql_numrows($result) < 1)){
      return 1; //Indicates username failure
   }

   /* Retrieve password from result, strip slashes */
   $dbarray = mysql_fetch_array($result);
   $dbarray['UserPW']  = stripslashes($dbarray['UserPW']);
   $password = stripslashes($password);

   /* Validate that password is correct */
   if($password == $dbarray['UserPW']){
      return 0; //Success! Username and password confirmed
   }
   else{
      return 2; //Indicates password failure
   }
}

PDO:

<?
function confirmUser($username, $password){
   global $conn;

   include("connection/conn.php");

   $sql = '
    SELECT   COALESCE(id,0) is_row
    FROM     user
    WHERE    UserID = ?
    LIMIT 1
';

$stmt = $conn->prepare($sql);
$stmt->execute(array('09185346d'));
$row = $stmt->fetch();

if ($row[0] > 0) {
       $sql = '
    SELECT   COALESCE(id,1) is_row
    FROM     user
    WHERE    UserPW = ?
    LIMIT 1
';
$stmt = $conn->prepare($sql);
$stmt->execute(array('asdasdsa'));
$row = $stmt->fetch();
    if ($row[0] > 0) 
    return 2;
    else
    return 0;
}
elseif ($row[0] = 0)
{return 1;}   



}

What is the problem ?? And is it necessary to include bind parameter in PDO??? THANKS

  • 写回答

1条回答 默认 最新

  • doue2666 2012-02-03 09:50
    关注

    Aside from your use of global and your include inside the function (you should investigate an alternative way of structuring your function not to do this), I would change the code as follows:

    $sql =
        'SELECT  id
        FROM     user
        WHERE    UserID = ?
        AND      UserPW = ?
        LIMIT 1';
    
    $stmt = $conn->prepare($sql);
    $stmt->execute(array(
        '09185346d',
        'asdasdsa'
    ));
    
    if ($stmt->rowCount() == 1) {
        return 0;
    }
    else {
        return 1;
    }
    

    Combing the queries to give a general Authentication error, instead of allowing people to trial valid usernames, and then valid passwords, and then using PDOStatements rowCount method do see if your row was returned.

    To answer your second part, it is not necessary to specifically use bindParam to prevent SQL injection.

    Here's a quick example of the difference between bindParam and bindValue

    $param = 1;
    
    $sql = 'SELECT id FROM myTable WHERE myValue = :param';
    $stmt = $conn->prepare($sql);
    

    Using bindParam

    $stmt->bindParam(':param', $param);
    $param = 2;
    $stmt->execute();
    

    SELECT id FROM myTable WHERE myValue = '2'

    Using bindValue

    $stmt->bindValue(':param', $param);
    $param = 2;
    $stmt->execute();
    

    SELECT id FROM myTable WHERE myValue = '1'

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题