gpt4 book ai didi

php - MySQL/phpMyAdmin 查询行计数(1 行)与 mysql_num_rows 的计数(0 行计数)

转载 作者:行者123 更新时间:2023-11-29 00:47:39 26 4
gpt4 key购买 nike

这是有问题的代码:

<?php // If search option is selected
include 'db_connect.php';

database_connect();
$limit = 5; // No of result per page
$criteria = 1; // Search criteria
$allResults=array(); // Array of results
$searchOption = $_REQUEST['searchoption'];
$searchField = $_REQUEST['searchfield'];
$searchField = str_replace('+',' ', $searchField) ;
$dbcolumn = "user_id";
$dbtable = "profiles";
$order = "user_id";

if ($searchField){
if ($searchOption == "People"){
$words = explode(' ', trim($searchField));
$searchField = $words[0];
$dbcolumn = "profile_display_name";
$dbtable = "profiles P, user_roles U, roles R";
$order = "profile_first_name";
$criteria = " P.user_id = U.user_id AND U.role_id = R.role_id
AND(`profile_display_name` LIKE '%$searchField%'
OR `profile_first_name` LIKE '%$searchField%'
OR `profile_last_name` LIKE '%$searchField%'
OR `profile_email` LIKE '%$searchField%'
OR `profile_state` LIKE '%$searchField%'
OR `profile_zip_code` LIKE '%$searchField%'
OR `business_summary` LIKE '%$searchField%')";
}
//focus here folks (my comment to readers, not actual part of code
else if ($searchOption == "Jobs"){
$dbcolumn = "job_title";
$dbtable = "job_postings";
$order = "`created_date` DESC";
$criteria = "(`job_title` LIKE '%$searchField%'
OR `job_description` LIKE '%$searchField%'
OR `client_name` LIKE '%$searchField%'
OR `city` LIKE '%$searchField%'
OR `state_abbr` LIKE '%$searchField%'
OR `zipcode` LIKE '%$searchField%')
AND `job_status` = 'Open'";
}
else if($searchOption == "News"){
$dbcolumn = "";
$dbtable = "";
$order = "";
$criteria = "1";
}

//Pagination
if($dbtable){
$rs = mysql_query("SELECT * FROM $dbtable WHERE $criteria");
$no_of_rec = mysql_num_rows($rs);
$page = ($_REQUEST['page'])? $_REQUEST['page'] : 1;
$no_of_pages = ceil($no_of_rec/$limit);
$offset = ($page - 1)*$limit;

echo "pagination #rows: $no_of_rec ";
}
}

// If the user enters a value in the search
if ($searchField){
$resultcount = 0;
if($dbtable) {
// Search query executes
$query = "SELECT * FROM $dbtable WHERE $criteria ";
$query .= "ORDER BY $order ";
$query .= "LIMIT $offset, $limit";
$result = mysql_query($query) or die(mysql_error());
$resultcount = mysql_numrows($result);

echo "query: $query ";
echo "result: $result ";
echo "result count: $resultcount ";
}

if ($resultcount <= 0){
$allResults[] = "No match found";
}
else{
// Get search results from database
while ($row = mysql_fetch_array($result)){
$allResults[]=$row;
}

}
}
else{
$allResults[] = "No value entered";
}
?>

这是 echo 语句显示的内容:分页 #rows: 0 查询:SELECT * FROM job_postings WHERE (`job_title` LIKE '%Help Wanted%' OR `job_description` LIKE '%Help Wanted%' OR `client_name` LIKE '%Help Wanted%' OR ` city` LIKE '%Help Wanted%' OR `state_abbr` LIKE '%Help Wanted%' OR `zipcode` LIKE '%Help Wanted%') AND `job_status` = 'Open' ORDER BY `created_date` DESC LIMIT 0, 5结果:资源 ID #36 结果计数:0

(我保留了 echo 打印输出,这样你就可以看到我看到了什么以及我是如何看到它的。)

当我在 MySQL 和 phpMyAdmin 中运行查询时,我得到了我正在寻找的行。

有人可以解释为什么 mysql_numrows() 认为有 0 个结果吗?

我已经检查了 PHP.net 并且我也用谷歌搜索了这个。到目前为止没有运气。

非常感谢。

最佳答案

这一行有误

 $resultcount = mysql_numrows($result); 

你应该调用 mysql_num_rows()

您可以选择使用此代码

$isExist = mysql_query("Select count(id) from ..."); 
$r = mysql_fetch_array($isExist);
if($r['COUNT(id)'] > 0){
//item exists
}else{
//item doesnt exist
}

关于php - MySQL/phpMyAdmin 查询行计数(1 行)与 mysql_num_rows 的计数(0 行计数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9931192/

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