PHP Mysqli没有错误,没有疑问

时间:2013-01-28 10:09:50

标签: php mysqli

我正在尝试使用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。我错过了错误报告吗?

3 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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";
}

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

答案 2 :(得分:0)

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

if ($stmt->execute()){

    $result = $stmt->affected_rows;

    if ($result) { echo "yay" } else { echo "boo"; }
}
else {
    printf("Execute error: %s", $stmt->error);
}