samona Posted June 26, 2008 Share Posted June 26, 2008 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); Quote Link to comment Share on other sites More sharing options...
rhodesa Posted June 26, 2008 Share Posted June 26, 2008 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 Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted June 26, 2008 Share Posted June 26, 2008 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 Quote Link to comment Share on other sites More sharing options...
samona Posted June 26, 2008 Author Share Posted June 26, 2008 The output from the code is "Resource id#4". That's why I named the topic Resource id#4. And yes, I'm trying to find out if there are any rows entered for the current day of the type. Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted June 26, 2008 Share Posted June 26, 2008 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 Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted June 26, 2008 Share Posted June 26, 2008 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() Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted June 26, 2008 Share Posted June 26, 2008 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. Quote Link to comment Share on other sites More sharing options...
fenway Posted June 26, 2008 Share Posted June 26, 2008 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. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted June 26, 2008 Share Posted June 26, 2008 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. Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted June 26, 2008 Share Posted June 26, 2008 ^ does curdate get the value of the current time? or the midnight time of the current day? Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted June 26, 2008 Share Posted June 26, 2008 From the documentation I linked to: Returns the current date as a value in 'YYYY-MM-DD' or YYYYMMDD format, depending on whether the function is used in a string or numeric context. Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted June 26, 2008 Share Posted June 26, 2008 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? Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted June 26, 2008 Share Posted June 26, 2008 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. Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted June 27, 2008 Share Posted June 27, 2008 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. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted June 27, 2008 Share Posted June 27, 2008 http://www.google.com/search?q=mysql+curdate Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.