您好我写了一个函数来检查我的数据库在输入之前是否有重复的名称。我尝试了三种不同的方法来捕获重复条目的数量,但是numrows总是返回为零。我最近的代码如下。
function check_item_name($item_id, $name) {
global $DBConnect;
$get_name = $DBConnect->stmt_init();
$query = "SELECT name
FROM item_name
WHERE item_id = ? AND name = ? AND end_date IS NULL ";
$get_name->prepare($query);
$get_name->bind_param("is", $item_id , $name);
$get_name->execute();
$get_name->bind_result($name);
$get_name->store_result();
$result = $get_name->get_result();
if($result->num_rows > 0){
$success = 1;
} else {
$success = 0;
}
return $success;
}
所以我现在尝试了这个,但是仍然是零,并且现在大约有十个相同的条目,所以绝对不应该为零。
function check_item_name($item_id, $name) {
global $DBConnect;
$get_name = $DBConnect->stmt_init();
$query = "SELECT count(1)
FROM item_id
WHERE item_id = ? AND name = ? AND end_date IS NULL ";
$get_name->prepare($query);
$get_name->bind_param("is", $item_id, $name);
$get_name->execute();
$get_name->bind_result($name);
$get_name->fetch();
if ($name){
$success = 1;
} else {
$success = 0;
}
var_error_log("------success-------------");
var_error_log($success);
return $success;
}
答案 0 :(得分:0)
许多道歉下面的代码完美无缺,我只需要调整我的ajax调用。谢谢你对YourCommonSense的回答。
function check_item_name($item_id, $name) {
global $DBConnect;
$get_name = $DBConnect->stmt_init();
$query = "SELECT count(1)
FROM item_id
WHERE item_id = ? AND name = ? AND end_date IS NULL ";
$get_name->prepare($query);
$get_name->bind_param("is", $item_id, $name);
$get_name->execute();
$get_name->bind_result($name);
$get_name->fetch();
if ($name){
$success = 1;
} else {
$success = 0;
}
return $success;
}