簡體   English   中英

MySQL / phpMyAdmin查詢行數(1行)vs mysql_num_rows的數(0行數)

[英]MySQL/phpMyAdmin Query Row Count (1 row) vs mysql_num_rows' Count (0 row count)

這是有問題的代碼:

<?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語句顯示的內容: pagination #rows: 0 query: 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 result: Resource id #36 result count: 0

(我將回聲打印輸出保持原樣,以便您可以看到所見以及所見。)

當我在MySQL和phpMyAdmin中運行查詢時,得到要查找的行。

有人可以解釋為什么mysql_numrows()認為有0個結果嗎?

我已經檢查過PHP.net,也已經對此進行了Google搜索。 到目前為止沒有運氣。

非常感謝。

這行有一個錯誤的類型

 $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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM