将mysql_real_escape转换为它的MySQLi等价物

时间:2015-11-14 19:09:12

标签: php mysqli

我有这段代码:

<?php
@include_once('set.php');
@include_once('steamauth/steamauth.php');
if(!isset($_SESSION["steamid"])) {
        Header("Location: index.php");
        exit;
}
$link = $_POST["link"];
$link = mysql_real_escape_string($link);
$steam = $_SESSION["steamid"];
mysql_query("UPDATE users SET `tlink`='$link' WHERE `steamid`='$steam'");
Header("Location: settings.php");
exit;
?>

我需要将其转换为MySQLi,因为它在当前状态下完全没用。改变&#34; mysql _&#34;到&#34; mysqli _&#34;根本不起作用。

1 个答案:

答案 0 :(得分:0)

我认为这就是你要找的东西。

<?php

require_once('set.php');
require_once('steamauth/steamauth.php');
if(!isset($_SESSION["steamid"])) {
    header("Location: index.php");
    exit();
}
$steam = $_SESSION["steamid"];

// Open a new connection to the MySQL server
$connection = new mysqli($host, $username, $password, $databaseName);

// Check connection
if($connection->connect_errno){
    // error
    die('Connect Error: ' . $connection->connect_error);
}

// Escapes special characters in a string for use in an SQL statement
$link = $connection->real_escape_string($_POST["link"]);

// Execute query
$connection->query("UPDATE users SET tlink='{$link}' WHERE steamid='{$steam}'");

// Close connection
$connection->close();
header("Location: settings.php");
exit();

?>