ecopetition Posted August 2, 2008 Share Posted August 2, 2008 Hi, I made an attachments feature for a forum software, and each post can have a maximum of one attachment. However, the way I check to see if the post has an attachment isn't completely working just now. Though it works when the post does have an attachment, when the post doesn't have an attachment it returns errors. $sql = "SELECT * FROM attachments WHERE post_id = ". $posts[$i]['post_id'] ." "; $result = $db->query($sql); while($row = $db->fetch_assoc($result)) { $attachments[0] = $row; } Each post has a unique ID, and if it has an attachment I check to see if it has a row in the attachments table of the database with the "post_id" column set to the post ID. Not every post has an attachment though, and therefore an according row in the attachments table (which returns an error). How can I stop an error from being returned when it doesn't have an attachment? Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/117837-unset-variable-errors/ Share on other sites More sharing options...
JasonLewis Posted August 2, 2008 Share Posted August 2, 2008 By checking how many rows are returned... $sql = "SELECT * FROM attachments WHERE post_id = ". $posts[$i]['post_id'] ." "; $result = $db->query($sql); if($db->num_rows($result) > 0){ //of course, add in your num_rows function here. //Its greater then 0, that means we have an attachment. while($row = $db->fetch_assoc($result)) { $attachments[0] = $row; } } Quote Link to comment https://forums.phpfreaks.com/topic/117837-unset-variable-errors/#findComment-606094 Share on other sites More sharing options...
DeanWhitehouse Posted August 2, 2008 Share Posted August 2, 2008 simple and quick way(not neccesarily good) you can turn error reporting off. error_reporting(0); i think Quote Link to comment https://forums.phpfreaks.com/topic/117837-unset-variable-errors/#findComment-606096 Share on other sites More sharing options...
ecopetition Posted August 2, 2008 Author Share Posted August 2, 2008 of course, add in your num_rows function here. Call me thick, but how do I do that ? Quote Link to comment https://forums.phpfreaks.com/topic/117837-unset-variable-errors/#findComment-606128 Share on other sites More sharing options...
ecopetition Posted August 2, 2008 Author Share Posted August 2, 2008 ? Quote Link to comment https://forums.phpfreaks.com/topic/117837-unset-variable-errors/#findComment-606183 Share on other sites More sharing options...
DeanWhitehouse Posted August 2, 2008 Share Posted August 2, 2008 try looking in the manual or google. mysql_num_rows Quote Link to comment https://forums.phpfreaks.com/topic/117837-unset-variable-errors/#findComment-606184 Share on other sites More sharing options...
JasonLewis Posted August 2, 2008 Share Posted August 2, 2008 Yeah, your database class should haves a function that returns mysql_num_rows() function num_rows($qry){ return mysql_num_rows($qry); } Just an example. Quote Link to comment https://forums.phpfreaks.com/topic/117837-unset-variable-errors/#findComment-606385 Share on other sites More sharing options...
cooldude832 Posted August 2, 2008 Share Posted August 2, 2008 I'm new to OOP projectfear and I'm confused why everyone writes those one liner functions when you could just say $object_var->mysql_num_rows($r); is that not the same thing? or do OOP people have a superiority complex and like to rename the functions to what they want? Quote Link to comment https://forums.phpfreaks.com/topic/117837-unset-variable-errors/#findComment-606395 Share on other sites More sharing options...
DeanWhitehouse Posted August 2, 2008 Share Posted August 2, 2008 why even assign it as an object why not just use mysql_num_rows();?? forgive me if it sounds stupid Quote Link to comment https://forums.phpfreaks.com/topic/117837-unset-variable-errors/#findComment-606399 Share on other sites More sharing options...
JasonLewis Posted August 2, 2008 Share Posted August 2, 2008 Well really, you could expand the num_rows() function that I have up there and make it more then just one line. You can call the function whatever you wish, it's just I personally find it easier to make it a bit smaller. Heck I know people that use the mysql_num_rows function as "nr". @Blade: As I said, you can make it more then one line. Oh and I really am not an OOP pro, I'm just okay at it. Quote Link to comment https://forums.phpfreaks.com/topic/117837-unset-variable-errors/#findComment-606403 Share on other sites More sharing options...
cooldude832 Posted August 2, 2008 Share Posted August 2, 2008 so its to give you flexibility should you need to return the integer from a different method? Quote Link to comment https://forums.phpfreaks.com/topic/117837-unset-variable-errors/#findComment-606406 Share on other sites More sharing options...
Stooney Posted August 2, 2008 Share Posted August 2, 2008 so its to give you flexibility should you need to return the integer from a different method? It gives you more flexibility in my opinion. $result=Db->query("select query here", 1); You could have Db->query return an already fetched result if you passed '1'. Then you don't need another line to fetch it. Of course you only use that when you only expect 1 row to be selected. (hope that made sense) Quote Link to comment https://forums.phpfreaks.com/topic/117837-unset-variable-errors/#findComment-606410 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.