Mysqli undefine变量

时间:2017-08-21 18:38:51

标签: php html mysql mysqli

您好我正在使用php和mysqli语句进行简单的crud项目。

首先一切都运行良好,但是例如mysqli_num_rows($result)会返回多行导致所有错误。

这是我的PHP代码

<?php

if(isset($_GET["email"]) && !empty(trim($_GET["email"]))){
// Include config file
require_once 'db.php';

// Prepare a select statement
$sql = "SELECT * FROM interns WHERE email = ?";

if($stmt = mysqli_prepare($con, $sql)){
    // Bind variables to the prepared statement as parameters
    mysqli_stmt_bind_param($stmt, "i", $param_id);

    // Set parameters
    $param_id = trim($_GET["email"]);

    // Attempt to execute the prepared statement
    if(mysqli_stmt_execute($stmt)){
        $result = mysqli_stmt_get_result($stmt);

        if(mysqli_num_rows($result) == 1){
            /* Fetch result row as an associative array. Since the result set
            contains only one row, we don't need to use while loop */
            $row = mysqli_fetch_array($result, MYSQLI_ASSOC);

            // Retrieve individual field value
            $firstname = $row["firstname"];
            $lastname = $row["lastname"];
            $cin = $row["cin"];
            $phone_number = $row["phone_number"];
            $address = $row["address"];
            $school = $row["school"];
            $intern_duration = $row["intern_duration"];
            $departement = $row["departement"];
            $cv = $row["cv"];
            $internship_report = $row["internship_report"];
        } else{
            // URL doesn't contain valid id parameter. Redirect to error page

        }

    } else{
        echo "Oops! Something went wrong. Please try again later.";
    }
}

// Close statement
mysqli_stmt_close($stmt);
} else{
// URL doesn't contain id parameter. Redirect to error page
header("location: error.php");
exit();
}
?>

我知道这可能是一个非常简单的错误,但它让我疯狂xD

1 个答案:

答案 0 :(得分:2)

mysqli_stmt_bind_param($stmt, "i", $param_id)对于字符串应该是s,用于电子邮件地址。 i代表“整数”。

您的查询可能会返回多行,因为(可能)有多行包含整数。

您还可以在查询中添加LIMIT 1,这可能有所帮助。