dtzhfb4869 2019-08-11 10:59
浏览 49
已采纳

检查输入框中的字符串是否等于SQL SERVER,PHP中的varbinary(max) SQL SERVER 2012

I'm having a problem with my login page.

I'm converting my password from string to varbinary(max), my problem now is how can I compare or verify if my string inputted for logging in is equal to the stored data in the table.

Example.

The value of my password before being converted is 12345. After being converted via CONVERT(varbinary(max),'12345'), the data now reflects as 0x3132333435.

How will I be able to verify if my inputted password (12345) in my input box is similar to the value if converted into varbinary(max) 0x3132333435?

I'm using SQL SERVER 2012, XAMPP, and SQLSRV for conncetion.

Thank you so much in advance.

This is my login.php

<?php
    session_start();
    include 'includes/conn.php';

    if (isset($_POST['login'])) {
        $username = $_POST['username'];
        $password = $_POST['password'];

        $sql = "
SELECT username,cast(password as varchar(max)) as password 
FROM usermasterfile 
WHERE username ='$username'";

        include 'query/sqlsrv_query-global.php';

        if (sqlsrv_num_rows($query) < 1) {
            $_SESSION['error'] = 'Cannot find account with the username';
        } else {
            $row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC);
            if ($password == $row['password']) {
                $_SESSION['employeeidno'] = $row['employeeidno'];
            }
            else{
                $_SESSION['error'] = 'Incorrect password';
            }
        }
    }
    else{
        $_SESSION['error'] = 'Input credentials first';
    }

    header('location: index.php');
?>

EDIT: FIXED IT!

SELECT *,cast(password as varchar(max)) as maxpassword 
FROM usermasterfile 
WHERE username ='$username'";````
  • 写回答

1条回答 默认 最新

  • dongpang2483 2019-08-11 11:11
    关注

    You can cast your varbinary back to varchar value as

    SELECT cast(0x3132333435 as varchar(max)) as varchar_value
    
    varchar_value
    -------------
    12345
    

    So, use the above casting in your code's comparison part.

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

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?