简体   繁体   English

如何将此 PDO 代码转换为 MySQLi?

[英]How do I convert this PDO code to MySQLi?

I'm quite new to PHP and MySQL and I try to learn how to change a code from PDO to MySQLi.我对 PHP 和 MySQL 很陌生,我尝试学习如何将代码从 PDO 更改为 MySQLi。 Its about a remember me function with a securitytoken and identifier for a login system that I found in the web.它是关于记住我的功能,带有我在网上找到的登录系统的安全令牌和标识符。 I would like to learn and understand how I can change the code from PDO to MySQLi.我想学习和了解如何将代码从 PDO 更改为 MySQLi。 I know in MySQLi there is a statement create and prepare, also I have to bind parameters and execute.我知道在 MySQLi 中有一个语句创建和准备,我也必须绑定参数并执行。 But in this case, I don't know how to start anyway.但在这种情况下,我无论如何都不知道如何开始。

$pdo = new PDO('mysql:host=localhost;dbname=dbname', 'root', '');
if (!isset($_SESSION['id']) && isset($_COOKIE['identifier']) &&
isset($_COOKIE['securitytoken'])) {
    $identifier = $_COOKIE['identifier'];
    $securitytoken = $_COOKIE['securitytoken'];

    $statement = $pdo->prepare("SELECT * FROM securitytokens WHERE identifier = ?");
    $result = $statement->execute(array($identifier));
    $securitytoken_row = $statement->fetch();

    if (sha1($securitytoken) !== $securitytoken_row['securitytoken']) {
        die('Maybe a stolen securitytoken.');
    } else {
        //Token was correct
        //Set an new token
        $neuer_securitytoken = random_string();
        $insert = $pdo->prepare("UPDATE securitytokens SET securitytoken = :securitytoken WHERE identifier = :identifier");
        $insert->execute(array('securitytoken' => sha1($neuer_securitytoken), 'identifier' => $identifier));
        setcookie("identifier", $identifier, time() + (3600 * 24 * 365)); //1 Year valid
        setcookie("securitytoken", $neuer_securitytoken, time() + (3600 * 24 * 365)); //1 Year valid

        //Loggin the user
        $_SESSION['id'] = $securitytoken_row['id'];
    }
}

Don't do it!不要这样做! PDO is simpler and offers more functionality. PDO 更简单并提供更多功能。

If you want to do it just to learn MySQLi then you should keep these things in mind:如果您只想学习 MySQLi,那么您应该记住以下几点:

  1. This is how you open the connection properly:这是您正确打开连接的方式:

     mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); $mysqli = new mysqli($host, $user, $pass, $db); $mysqli->set_charset($charset);
  2. There is no bind-in-execute in MySQLi. MySQLi 中没有执行中的绑定。 In fact there is no bind by value at all!事实上,根本没有价值绑定! You can only bind by reference and you have to specify the type of the value.您只能通过引用绑定,并且必须指定值的类型。

     $statement = $mysqli->prepare("SELECT * FROM securitytokens WHERE identifier = ?"); $statement->bind_param('i', $identifier); // i for integer; s for string. $statement->execute();
  3. MySQLi has no named placeholders, so you need to use positional ones only. MySQLi 没有命名占位符,所以你只需要使用位置占位符。

     $insert = $mysqli->prepare("UPDATE securitytokens SET securitytoken = ? WHERE identifier = ?"); $sha1ResultDueToPassByRef = sha1($neuer_securitytoken); $insert->bind_param('si', $sha1ResultDueToPassByRef, $identifier); $insert->execute();
  4. The fetch method in MySQLi works totally differently and it returns boolean. MySQLi 中的fetch方法完全不同,它返回布尔值。 To get the same outcome as PDO's fetch() you would need to get_result() and then use fetch_array() or fetch_assoc()要获得与PDO 的fetch()相同的结果,您需要get_result()然后使用fetch_array()fetch_assoc()

     $securitytoken_row = $statement->get_result()->fetch_array();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM