准备好的语句检查行是否存在

时间:2017-01-03 09:41:21

标签: php mysqli

我是Php的Prepared Statements的新手,并且想知道如果一行已经存在,你最好如何检查,因为我似乎在这个阶段感到困惑:

<?php 
include '../config.php'; 

$stmt = $conn->prepare("INSERT INTO users (email, password) VALUES (?, ?)");
$stmt->bind_param("ss", $email, $password);

if(mysqli_num_rows($stmt) > 0) {
    $email = $_POST['email'];
    $password = $_POST['password'];
    $stmt->execute();
    header('Location: ../login.php');   
} else {
    echo 'user already exists';
}
$stmt->close();
$conn->close();
?>

上面的内容返回else语句,如果我将它们切换,它将再次插入,使用else语句并插入记录但仍未检查。

**更新**

以下是我在下面的帮助后看到的更新代码。

<?php 
include '../config.php'; 

ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);

$email = $_POST['email'];
$password = $_POST['password'];

$stmt_check = $conn->prepare("SELECT * FROM users WHERE email =?");
$stmt_check->bind_param("s", $email);
$stmt_check->execute();
if($stmt_check->num_rows > 0) {
    echo 'user already exists';
} else {
    $stmt = $conn->prepare("INSERT INTO users (email, password) VALUES (?, ?)");
    $stmt->bind_param("ss", $email, $password);
    $stmt->execute();
    // header('Location: ../login.php');
}    
$stmt->close();
$conn->close();
?>

4 个答案:

答案 0 :(得分:4)

mysqli_num_rows 适用于SELECT语句。

$stmt_check = $conn->prepare("SELECT * FROM users WHERE email =? AND password =?");
$stmt_check->bind_param("ss", $email, $password);
$stmt_check->execute();
if(mysqli_num_rows($stmt_check) > 0)

更新代码

<?php 

include '../config.php'; 

$email = $_POST['email'];
$password = $_POST['password'];

$stmt_check = $conn->prepare("SELECT * FROM users WHERE email =? AND password =?");
$stmt_check->bind_param("ss", $email, $password);
$stmt_check->execute();
if($stmt_check->num_rows > 0){
  echo 'user already exists';
} else {
  $stmt = $conn->prepare("INSERT INTO users (email, password) VALUES (?, ?)");
  $stmt->bind_param("ss", $email, $password);
  $stmt->execute();
  header('Location: ../login.php');
}

$stmt->close();
$conn->close();
?>

快速链接

哪个州,

  

此命令仅对SELECT或SHOW等语句有效   返回实际结果集。检索受影响的行数   通过INSERT,UPDATE,REPLACE或DELETE查询,使用   mysql_affected_rows()

修改1

更改

if(mysqli_num_rows($stmt_check) > 0){

if($stmt_check->num_rows > 0){

参见PHP mysqli_num_rows() Function

Example2

答案 1 :(得分:1)

这是我的更新代码,请尝试

  <?php 

include '../config.php'; 

$email = $_POST['email'];
$password = $_POST['password'];

$stmt_check = $conn->prepare("SELECT * FROM users WHERE email =? AND password =?");
$stmt_check->bind_param("ss", $email, $password);
$stmt_check->execute();
$stmt_check->store_result();
$numberofrows = $stmt_check->num_rows;

if(($numberofrows) > 0)
  echo 'user already exists';
} else {
  $stmt = $conn->prepare("INSERT INTO users (email, password) VALUES (?, ?)");
  $stmt->bind_param("ss", $email, $password);
  $stmt->execute();
  header('Location: ../login.php');
}

$stmt->close();
$conn->close();
?>

答案 2 :(得分:0)

看看mysqli_stmt_affected_rows()

<?php include '../config.php'; 

$stmt = $conn->prepare("INSERT INTO users (email, password) VALUES (?, ?)");
$stmt->bind_param("ss", $_POST['email'], $_POST['password']);
$stmt->execute();

if(mysqli_stmt_affected_rows($stmt) > 0)
{
    header('Location: ../login.php');   
}
else
{
    echo 'user already exists';
}

$stmt->close();
$conn->close();
?>

答案 3 :(得分:-1)

<?php include '../config.php'; 

$stmt = $conn->prepare("INSERT INTO users (email, password) VALUES (?, ?)");
$stmt->bind_param("ss", $_POST['email'], $_POST['password']);
$result = $stmt->execute(); // this return a bool: true if row affected otherwise false

if($result)
{
    header('Location: ../login.php');   
}
else
{
    echo 'user already exists';
}

$stmt->close();
$conn->close();
?>