tags:

views:

115

answers:

6

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

    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'];

}
A: 

return $num['NUM'];

means return the 'num' part of the array; Change 'NUM' to 1 or 2 and then try

NebNeb
`mysql_fetch_array()` returns an array with both column numbers AND column names. `mysql_fetch_assoc()` returns only an array with column names.
Chacha102
@NebNeb Nope, `mysql_fetch_array` fetches both the numeric and the associative part by default.
Pekka
By default, `mysql_fetch_array` returns the result as numerical **and** associative array. So the array access should work imo.
Felix Kling
+1  A: 

For starters, add

if (!$result) echo mysql_error(); 

after each mysql_query() call to see whether there are any errors. I'm pretty sure there are.

Pekka
Tried, no errors.
well post them then
c0mrade
@c0mrade how can he post no errors? :D
Pekka
@Pekka sorry still upset at someone down voting me in like 20 sec .. didn't read it properly
c0mrade
Should I put this after EVERY mysql_query() to see the error?
@hypnocode yup.
Pekka
Nothing, I put them after each one.
If you don't like that use PDO ( http://docs.php.net/pdo ) and set `$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);`. Then an exception is thrown each time something goes wrong. Not everybody likes exceptions, but imo at least it's a bit harder to "ignore" them or to forget the error handling.
VolkerK
Thanks, I'll add it to my list of things to try.
+1  A: 

Your queries are not formatter properly they are :

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

They should be :

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

You have an extra semi-colon in each query. and use or die mysql_error() to print out the error.

Also this part :

$num = mysql_fetch_array ($result);

  return $num['NUM'];

I'd replaced with :

$num = mysql_fetch_array ($result);

extract($num);

  return $NUM;//if this is your field name
c0mrade
That is proper SQL. It delimited queries. While it is not necessary, it is not the cause of the problem.
Chacha102
This shouldn't be a problem. `;` is the query deliminator.
Felix Kling
why the minus vote I'm offering a man few solutions, give guy something instead of just minusing voted dammit
c0mrade
@c0mrade: I agree that some people are very fast in downvoting other answers...sometimes this is really ridiculous.
Felix Kling
yea .. so far I've given up offering some more solutions .. I simply don't care anymore to write anythign .. peace
c0mrade
Because your 'solution' will just waste the OP's time.
Tangrs
What's the point of using extract()? I would understand using mysql\_result() since it's a single row with only one value, but extract()?
VolkerK
@c0mrade: a downvote is nothing personal. It's just a sign that someone disagrees with the answer, and it was totally justified in your case. But hey, don't take it too seriously. It's just a game.
stereofrog
+3  A: 

Try some debug output in your function, e.g.

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'];
}
VolkerK
this covers too much of his code but still +1 he will get the error eventually at the end, he'd be faster of changing "suspicious" bits of code
c0mrade
I'm trying Pekka's post and if that doesn't work I'll try yours. Thanks!!!
I hope it "resembles" the original function just enough to be "comprehensible" ;-) Keep in mind that you usually neither want to bail out so quickly (using die()) nor reveal the actual mysql error string to each and every user in production code ...it's just an example.
VolkerK
And thanks for adding the switch...I never knew you could do that in PHP.
Thanks! This is getting me closer...for some reason it's just passing in a year for the date. I'll let you know if I find a solution.
Thank you very much, this helped me find out what was going wrong. I posted my solution. I would vote this up, but I don't have 15 rep points yet.
+1  A: 

Fixed it...dumb mistake. I'm so new to PHP and MySQL, sorry.

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

should have been:

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

Thanks all for the help and I now know how to debug properly!

Glad to hear that you solved it...
Felix Kling
Thank you very much for your help.
A: 

You're trying to reference a numeric index [n] with an associative index ['s']. Either specify this to your query

mysql_fetch_array($result, MYSQL_BOTH)

Or just do

mysql_fetch_assoc($result);

Which will let you reference indices by association

Ben