php-mysql检查记录是否存在



我在检查数据库中是否存在记录时遇到问题。我想它的问题是不正确使用mysqli_num_rows

<?php
$mysqli = new mysqli("xxxxxx", "xxxxx", "xxxxxx", "xxxxx");
/* check connection */
if ($mysqli->connect_errno) {
    die("Connect failed: %sn" . $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();
?>

尝试使用:

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

相关内容

  • 没有找到相关文章

最新更新