PHP function returns ZERO

I am having problems with this PHP function. It keeps returning zero, but I know the SQL statement works because I requested it myself. Any ideas what I'm doing wrong? The last line makes no sense to me ... I am editing this code that someone else wrote. This function should return a number in hundreds, assuming that the date is in March. Thank!

    function getCountBetweenDays($day1,$day2,$service)

{

    global $conn;

    if ($service==1){

$query = "SELECT COUNT(*) as NUM FROM `items` WHERE `modified` BETWEEN '$day1 00:00:00' AND '$day2 23:59:59';";}



    elseif($service==2){

$query = "SELECT COUNT(*) as NUM FROM `items` WHERE `modified` BETWEEN '$day1 00:00:00' AND '$day2 23:59:59';";}



        elseif($service==3){

$query = "SELECT COUNT(*) as NUM FROM `items` WHERE `modified` BETWEEN '$day1 00:00:00' AND '$day2 23:59:59';";}

$result = mysql_query($query,$conn);

  $num = mysql_fetch_array ($result);

  return $num['NUM'];

}
+3
source share
6 answers

Try debugging in your function like

function getCountBetweenDays($day1,$day2,$service)
{
  global $conn;

  switch($service) {
    case 1:
      $query = "SELECT COUNT(*) as NUM FROM `items` WHERE `modified` BETWEEN '$day1 00:00:00' AND '$day2 23:59:59'";
      break;
    case 2:
      $query = "SELECT COUNT(*) as NUM FROM `items` WHERE `modified` BETWEEN '$day1 00:00:00' AND '$day2 23:59:59'";
      break;
    case 3:
      $query = "SELECT COUNT(*) as NUM FROM `items` WHERE `modified` BETWEEN '$day1 00:00:00' AND '$day2 23:59:59'";
      break;
    default:
      die('unknown value for $service');
  }

  echo '<pre>Debug: $query=', htmlspecialchars($query), '</pre>';
  $result = mysql_query($query,$conn) or die('mysql_query failed: '.htmlspecialchars(mysql_error($conn)));
  echo '<pre>Debug: numrows=', mysql_num_rows($result), '</pre>';
  $num = mysql_fetch_array($result);

  return $num['NUM'];
}
+3
source

To start, add

if (!$result) echo mysql_error(); 

after each call mysql_query()to see if there are any errors. I am sure there are.

+1
source

:

"SELECT COUNT(*) as NUM FROM `items` WHERE `modified` BETWEEN '$day1 00:00:00' AND '$day2 23:59:59';";

:

"SELECT COUNT(*) as NUM FROM `items` WHERE `modified` BETWEEN '$day1 00:00:00' AND '$day2 23:59:59'"; 

. or die mysql_error() .

:

$num = mysql_fetch_array ($result);

  return $num['NUM'];

:

$num = mysql_fetch_array ($result);

extract($num);

  return $NUM;//if this is your field name
+1

... . PHP MySQL, .

getCountBetweenDays(2010-3-1,2010-3-28,CONT_ALL_SERVICE);

:

getTweetCountBetweenDays('2010-3-1','2010-3-28',CONT_ALL_SERVICE);

Thank you all for your help, and now I know how to properly debug!

+1
source

You are trying to reference the numeric index [n] with the associative index ['s']. Or indicate this in your request

mysql_fetch_array($result, MYSQL_BOTH)

Or simply

mysql_fetch_assoc($result);

That will allow you to refer to association indexes

0
source

return $ num ['NUM'];

means that returns the 'num' part of the array; Change "NUM" to 1 or 2, then try

-1
source

Source: https://habr.com/ru/post/1739218/


All Articles