0

我正在尝试使用 mysqli 而不是 mysql 查询,但它不起作用。mysqli:

$mysqli->connect($db1['host'], $db1['user'], $db1['password'], $db1['database']);
if ($mysqli->connect_errno) {
        printf("Connect failed: %s\n", $mysqli->connect_error);
        exit();
    }

没有错误。如果我尝试这个查询:

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

    $random_salt = hash('sha512', uniqid(mt_rand(1, mt_getrandmax()), true));

    $password = hash('sha512', $password.$random_salt);

    if ($insert_stmt = $mysqli->prepare("INSERT INTO members (username, email, password, salt) VALUES (?, ?, ?, ?)")) {    
       $insert_stmt->bind_param('ssss', $username, $email, $password, $random_salt); 

       $insert_stmt->execute();
    }
    echo "Success";
}

没有插入任何内容,mysqli 错误没有错误。表结构正确,表示成功。我是mysqli的新手,我习惯了mysql。错误报告有什么我错过的吗?

4

3 回答 3

0

最好试试这个,它来自 php 手册

<?php
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) 
{
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli- >connect_error;
}
于 2013-01-28T10:17:14.150 回答
0

你必须这样做

$password = hash('sha512', $password.$random_salt);

$insert_stmt = $mysqli->prepare("INSERT INTO members (username, email, password, salt) VALUES (?, ?, ?, ?)");     
$insert_stmt->bind_param('ssss', $username, $email, $password, $random_salt); 

if($insert_stmt->execute())
{
    echo "Success";
}

实际上,您首先检查查询,然后绑定参数,因为它只是显示Success.

于 2013-01-28T10:19:55.650 回答
0

你可以做 $stmt->execute(); 在这样的 if 循环中:

if ($stmt->execute()){

    $result = $stmt->affected_rows;

    if ($result) { echo "yay" } else { echo "boo"; }
}
else {
    printf("Execute error: %s", $stmt->error);
}
于 2013-01-28T13:00:06.037 回答