gpt4 book ai didi

php - 我的 php 脚本在执行返回零 (0) 行的 mysql 选择查询后停止处理和输出

转载 作者:行者123 更新时间:2023-11-29 13:41:25 26 4
gpt4 key购买 nike

下面是我的代码。当我尝试插入已存在的用户时,查询返回一行并且脚本工作正常。但是当查询返回零 row 时,脚本会在下面代码中注释为“STOPS HERE”的点处停止。请我真的不明白这个问题,而且我是 php 新手。甚至 if 之外的 echo 语句也不会被执行。

<?php
session_start();
$_SESSION["error"] = "";
include "dbconnection.php";

$_SESSION["validUser"] = 4;

$validUser = $_SESSION["validUser"];


extract($_POST);

if($courseSession == ""){
echo "You must Select a course session ";
}
else {
$query = "SELECT * FROM courses WHERE student = $validUser AND course='$courseSession'";
$result = mysql_query($query, $db) or die(mysql_error($db));

if(!$result){
echo " no results have been returned testing "; // used for testing
}
else {
echo " Results returned but don't understand \n Number rows = "; // used for testing
echo mysql_num_rows($result);

} //STOPS HERE, here is where the script stops after query above returns 0 row.
$check = mysql_num_rows($result) or die(mysql_error($db));

echo " after check course ";
if($check){

echo "You have already registered for this session ";


}

else if(!$check){

$query = "INSERT INTO courses
(course, student, groupId)

VALUES
('$courseSession', '$validUser', '$group')";

$res = mysql_query($query, $db) or die(mysql_error($db));
if(!$res){
echo "Error Registering Course ";
}
else{
echo "Successfully registered for this session ";
}
}
}


echo " TESting testing ";


?>

最佳答案

您的代码中的这一行有问题:

$check = mysql_num_rows($result) 或 die(mysql_error($db));

当它运行时,mysql_num_rows($result) 返回 0,在这种情况下其计算结果与 false 相同,这就是为什么 die() 被执行但没有任何结果之后运行。

关于php - 我的 php 脚本在执行返回零 (0) 行的 mysql 选择查询后停止处理和输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18032892/

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