Jump to content

Resource id#4


samona

Recommended Posts

Hi,

 

I have the following code snippet in my program and i'm getting an error Recource id#4.  Something about not the right resource type in the mysql_num_rows($result) line.  Can anyone help?

 

 

 

 

mysql_select_db("db", $connection);
$query = 'SELECT * FROM Table WHERE ((DATE_FORMAT(Table.Current_Date, \'%Y %m %d\')) = (DATE_FORMAT(NOW(), \'%Y %m %d\')))';

$result = mysql_query($query, $connection);
$numRows = mysql_num_rows($result) or die(mysql_error() . ' in ' . $result);

Link to comment
https://forums.phpfreaks.com/topic/112075-resource-id4/
Share on other sites

change

$result = mysql_query($query, $connection);
$numRows = mysql_num_rows($result) or die(mysql_error() . ' in ' . $result);

to

$result = mysql_query($query, $connection) or die(mysql_error() . ' in ' . $query);
$numRows = mysql_num_rows($result);

 

and post what the MYSQL error is

Link to comment
https://forums.phpfreaks.com/topic/112075-resource-id4/#findComment-575317
Share on other sites

Hi,

 

I have the following code snippet in my program and i'm getting an error Recource id#4.  Something about not the right resource type in the mysql_num_rows($result) line.  Can anyone help?

 

 

 

 

mysql_select_db("db", $connection);
$query = 'SELECT * FROM Table WHERE ((DATE_FORMAT(Table.Current_Date, \'%Y %m %d\')) = (DATE_FORMAT(NOW(), \'%Y %m %d\')))';

$result = mysql_query($query, $connection);
$numRows = mysql_num_rows($result) or die(mysql_error() . ' in ' . $result);

 

 

Are you trying to find rows in your database whose date corresponds to the current day? If so, there's an easier way(and less buggy). Let me know if thats correct

Link to comment
https://forums.phpfreaks.com/topic/112075-resource-id4/#findComment-575321
Share on other sites

Ok do it like this...

 

I have a get_midnight function I've made. and I put in a time value and it spits out the value of midnight of that day. So what you do it get the midnight value and then do a search in the database of any value that is > the midnight value. That way you'll find everything that has a timestamp of today.

function get_midnight($timestamp)
{
  $day = date("j", $timestamp);
  $month = date("F", $timestamp);
  $year = date("Y", $timestamp);

  $midnight = strtotime("$month $day $year");

  return $midnight;
}

 

so then your query would be

SELECT * FROM Table WHERE CurrentDate > '$midnight' 

 

provided currentdate is the value of the row that you want, if it was done today

 

 

EDIT: the output is referring to the resource probably because it was done incorrectly. Come to think of it, yea, the only reason it's ouputting is because there's an error in the query

Link to comment
https://forums.phpfreaks.com/topic/112075-resource-id4/#findComment-575352
Share on other sites

It's best to leave logic like that within the database; however the OP's code doesn't do it as optimally as it could.

 

It's always worth checking out the available functions in the documentation for this type of thing:

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_curdate

 

select ... where table.date_column >= CURDATE()

Link to comment
https://forums.phpfreaks.com/topic/112075-resource-id4/#findComment-575363
Share on other sites

It's best to leave logic like that within the database; however the OP's code doesn't do it as optimally as it could.

 

It's always worth checking out the available functions in the documentation for this type of thing:

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_curdate

 

select ... where table.date_column >= CURDATE()

 

I have to disagree ONLY because if you have the raw date format in the database, it's alot easier to figure out what kind of data you need by using php functions(alot friendlier and not as hard to figure out) and then search the db accordingly. But both ways work. It's driving stick vs driving manual.

Link to comment
https://forums.phpfreaks.com/topic/112075-resource-id4/#findComment-575365
Share on other sites

It's best to leave logic like that within the database; however the OP's code doesn't do it as optimally as it could.

 

It's always worth checking out the available functions in the documentation for this type of thing:

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_curdate

 

select ... where table.date_column >= CURDATE()

 

I have to disagree ONLY because if you have the raw date format in the database, it's alot easier to figure out what kind of data you need by using php functions(alot friendlier and not as hard to figure out) and then search the db accordingly. But both ways work. It's driving stick vs driving manual.

I disagree -- you're potentially doing a lot more work outside the DB, which is likely very low.

Link to comment
https://forums.phpfreaks.com/topic/112075-resource-id4/#findComment-575375
Share on other sites

function get_midnight($timestamp)
{
  $day = date("j", $timestamp);
  $month = date("F", $timestamp);
  $year = date("Y", $timestamp);

  $midnight = strtotime("$month $day $year");

  return $midnight;
}

 

vs.

 

  where `column` >= curdate()

 

Good rule of thumb, the less code you have the easier it is to maintain as long as it's still readable, which it clearly is in this case.

 

You also avoid any problems where the apache and mysql servers might not be the same physical machine and could potentially have different times.

Link to comment
https://forums.phpfreaks.com/topic/112075-resource-id4/#findComment-575386
Share on other sites

function get_midnight($timestamp)
{
  $day = date("j", $timestamp);
  $month = date("F", $timestamp);
  $year = date("Y", $timestamp);

  $midnight = strtotime("$month $day $year");

  return $midnight;
}

 

vs.

 

  where `column` >= curdate()

 

Good rule of thumb, the less code you have the easier it is to maintain as long as it's still readable, which it clearly is in this case.

 

 

were you saying my code wasnt readable?

Link to comment
https://forums.phpfreaks.com/topic/112075-resource-id4/#findComment-575404
Share on other sites

They're both readable and they both perform the same function.  The difference is that yours has greater volume and also references a function.  More code means more keystrokes which means more chances for mistakes.  The fact that it's a function also means I will likely have to cross-reference points in the code to figure out just what this get_midnight does.

 

Doing the work in MySQL is shorter and puts everything you need right there in front of you.  Hence it's easier to maintain.

 

I made the general statement that a smaller volume of code is easier to maintain given that it is still readable.

 

You can condense a 100 line program into 20 lines of unreadable "super-code" that nobody can decipher.  Doing something like that is counter-productive.

Link to comment
https://forums.phpfreaks.com/topic/112075-resource-id4/#findComment-575419
Share on other sites

 

You can condense a 100 line program into 20 lines of unreadable "super-code" that nobody can decipher.  Doing something like that is counter-productive.

 

 

yeah, thats when it's not good. If you're giving code to someone who's more fluent in php, it would be better to make the function bc if I dont know what curdate does(nor can find out) then I'd have no way of figuring out how to manipulate it to do other things.

Link to comment
https://forums.phpfreaks.com/topic/112075-resource-id4/#findComment-575641
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.