php mysql检查记录是否存在

时间:2014-04-28 11:56:18

标签: php mysql sql mysqli mysql-num-rows

检查数据库中是否存在记录时出现问题。我猜错了使用mysqli_num_rows

的问题
<?php

$mysqli = new mysqli("xxxxxx", "xxxxx", "xxxxxx", "xxxxx");

/* check connection */
if ($mysqli->connect_errno) {
    die("Connect failed: %s\n" . $mysqli->connect_error);
}

$stmt = $mysqli->prepare("INSERT INTO TEST_CHARS (CHAR_NAME) VALUES(?)");
$stmt->bind_param('s', $nick);
$nick  = $_POST['nick'];
$query = mysqli_query("SELECT CHAR_NAME FROM TEST_CHARS WHERE CHAR_NAME ='$nick';");
$count = mysqli_num_rows($query);

if ($count > 0)
{
    die('Error, character exist in database');
}
else
{
    $stmt->execute();
    echo "Character $nick was added successfully";
}
$mysqli->close();

?>

1 个答案:

答案 0 :(得分:-1)

尝试:

$query = mysqli_query($mysqli, "SELECT CHAR_NAME FROM TEST_CHARS WHERE CHAR_NAME ='$nick';");