gpt4 book ai didi

php - Mysqli undefined variable

转载 作者:行者123 更新时间:2023-11-27 23:11:27 25 4
gpt4 key购买 nike

您好,我正在使用 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

最佳答案

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

您的查询可能返回不止一行,因为(可能)不止一行包含整数。

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

关于php - Mysqli undefined variable ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45803677/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com