matthewhaworth Posted August 27, 2007 Share Posted August 27, 2007 <?php function numrows($table, $condition) { $sql = "SELECT * FROM " . $table . " WHERE " . $condition; $query = $this->_db->query($sql); return ($query != false) ? $query:"Error in function numrows"; } ?> I'm just not confident with the ? operator. Quote Link to comment https://forums.phpfreaks.com/topic/66812-solved-will-this-work-wrapped-function-for-numrows/ Share on other sites More sharing options...
teng84 Posted August 27, 2007 Share Posted August 27, 2007 have you tried it ??? Quote Link to comment https://forums.phpfreaks.com/topic/66812-solved-will-this-work-wrapped-function-for-numrows/#findComment-334893 Share on other sites More sharing options...
matthewhaworth Posted August 27, 2007 Author Share Posted August 27, 2007 have you tried it ??? I'd have to call the class and set everything up.. or use test data, when I was hoping for a quick answer on here? Quote Link to comment https://forums.phpfreaks.com/topic/66812-solved-will-this-work-wrapped-function-for-numrows/#findComment-334894 Share on other sites More sharing options...
teng84 Posted August 27, 2007 Share Posted August 27, 2007 you use the class within your function so iguess it wont work $query = $this->_db->query($sql); you have to declare the class global if you want it that way or declare it inside your functions Quote Link to comment https://forums.phpfreaks.com/topic/66812-solved-will-this-work-wrapped-function-for-numrows/#findComment-334896 Share on other sites More sharing options...
Jessica Posted August 27, 2007 Share Posted August 27, 2007 Look up the ternary operator...I don't think you're using it right. Just use an if(){}else{}... it reads better. Quote Link to comment https://forums.phpfreaks.com/topic/66812-solved-will-this-work-wrapped-function-for-numrows/#findComment-334897 Share on other sites More sharing options...
teng84 Posted August 27, 2007 Share Posted August 27, 2007 Look up the ternary operator...I don't think you're using it right. Just use an if(){}else{}... it reads better. whats wrong with that???????? Quote Link to comment https://forums.phpfreaks.com/topic/66812-solved-will-this-work-wrapped-function-for-numrows/#findComment-334899 Share on other sites More sharing options...
Jessica Posted August 27, 2007 Share Posted August 27, 2007 Oh..and: I'd have to call the class and set everything up.. or use test data, when I was hoping for a quick answer on here? You want us to do the hard part of thinking it out for you, so you don't have to actually use the code you've written? Quote Link to comment https://forums.phpfreaks.com/topic/66812-solved-will-this-work-wrapped-function-for-numrows/#findComment-334901 Share on other sites More sharing options...
Jessica Posted August 27, 2007 Share Posted August 27, 2007 I don't know, it just doesn't look right...but I never use ternary because it never looks right. It's too confusing. It's like using ASP style tags. Quote Link to comment https://forums.phpfreaks.com/topic/66812-solved-will-this-work-wrapped-function-for-numrows/#findComment-334902 Share on other sites More sharing options...
teng84 Posted August 27, 2007 Share Posted August 27, 2007 I don't know, it just doesn't look right...but I never use ternary because it never looks right. It's too confusing. It's like using ASP style tags. funny lol you said its wrong but then you dont know whats wrong hahahah ternary gives shorter code but slower output i guess Quote Link to comment https://forums.phpfreaks.com/topic/66812-solved-will-this-work-wrapped-function-for-numrows/#findComment-334906 Share on other sites More sharing options...
Jessica Posted August 27, 2007 Share Posted August 27, 2007 I said I didn't THINK it was right. You're way too concerned with your speed in situations when it is not important. Faster code does not give you a larger organ you know. Quote Link to comment https://forums.phpfreaks.com/topic/66812-solved-will-this-work-wrapped-function-for-numrows/#findComment-334910 Share on other sites More sharing options...
teng84 Posted August 27, 2007 Share Posted August 27, 2007 yah but i use ternary in some case Quote Link to comment https://forums.phpfreaks.com/topic/66812-solved-will-this-work-wrapped-function-for-numrows/#findComment-334913 Share on other sites More sharing options...
matthewhaworth Posted August 27, 2007 Author Share Posted August 27, 2007 Oh..and: I'd have to call the class and set everything up.. or use test data, when I was hoping for a quick answer on here? You want us to do the hard part of thinking it out for you, so you don't have to actually use the code you've written? No. I simply did not know if I was using the 'ternary' operator correctly. you use the class within your function so iguess it wont work $query = $this->_db->query($sql); you have to declare the class global if you want it that way or declare it inside your functions The function is used within the class. The $_db is a mysqli object. Quote Link to comment https://forums.phpfreaks.com/topic/66812-solved-will-this-work-wrapped-function-for-numrows/#findComment-334914 Share on other sites More sharing options...
Jessica Posted August 27, 2007 Share Posted August 27, 2007 It would have been faster I bet to have tried it out by now Or at least switched it to an if-else. According to the manual http://us3.php.net/ternary I think it will work. Quote Link to comment https://forums.phpfreaks.com/topic/66812-solved-will-this-work-wrapped-function-for-numrows/#findComment-334916 Share on other sites More sharing options...
matthewhaworth Posted August 27, 2007 Author Share Posted August 27, 2007 It would have been faster I bet to have tried it out by now Or at least switched it to an if-else. According to the manual http://us3.php.net/ternary I think it will work. *sigh*. I know. I have switched to an if statement. function numrows($table, $condition) { $sql = "SELECT * FROM " . $table . " WHERE " . $condition; $query = $this->_db->query($sql); //return ($query != false) ? $query:"Error in numrows"; if ($query != false) { return $query; } else { return "Error in numrows"; } } Thanks for the argument guys. . Note: the weird formatting accorded after I'd posted it.. Quote Link to comment https://forums.phpfreaks.com/topic/66812-solved-will-this-work-wrapped-function-for-numrows/#findComment-334919 Share on other sites More sharing options...
teng84 Posted August 27, 2007 Share Posted August 27, 2007 id rather do this, this way if ($query != false) { $result =$query; } else{ $result = "Error in numrows"; } return $result ; Quote Link to comment https://forums.phpfreaks.com/topic/66812-solved-will-this-work-wrapped-function-for-numrows/#findComment-334926 Share on other sites More sharing options...
dbo Posted August 27, 2007 Share Posted August 27, 2007 Hrmm. This is not at all how I would do this. Most database interfaces you're going to use have a function for number of rows... no need to try to reinvent it. Furthermore, the SELECT * is going to be inefficient and if you're querying multiple tables you'd have to pass it in as "table1, table2, table3" or something. Why not just set something up like this: mysql.php mssql.php oracle.php etc... then you have your main db class that includes the appropriate db class (above). If you change databases you swap out the include in that single file and move on. By doing this you can use the native mysql_num_rows function, etc. Quote Link to comment https://forums.phpfreaks.com/topic/66812-solved-will-this-work-wrapped-function-for-numrows/#findComment-334928 Share on other sites More sharing options...
Jessica Posted August 27, 2007 Share Posted August 27, 2007 Heh I never even paid that much attention - dbo is right, you are sort of re-inventing the wheel. Quote Link to comment https://forums.phpfreaks.com/topic/66812-solved-will-this-work-wrapped-function-for-numrows/#findComment-334931 Share on other sites More sharing options...
matthewhaworth Posted August 27, 2007 Author Share Posted August 27, 2007 id rather do this, this way if ($query != false) { $result =$query; } else{ $result = "Error in numrows"; } return $result ; I've noticed a lot of people do that, any reason why, and weren't you the person complaining about longer processing.. that'd take longer wouldn't it? Quote Link to comment https://forums.phpfreaks.com/topic/66812-solved-will-this-work-wrapped-function-for-numrows/#findComment-334936 Share on other sites More sharing options...
dbo Posted August 27, 2007 Share Posted August 27, 2007 I think returning different types from a single function is stupid... even if PHP will let you do it. If you're returning a result set... return a result set... at the very worst you should return a result set or false... not a result set or some string you have to test for. Quote Link to comment https://forums.phpfreaks.com/topic/66812-solved-will-this-work-wrapped-function-for-numrows/#findComment-334938 Share on other sites More sharing options...
teng84 Posted August 27, 2007 Share Posted August 27, 2007 id rather do this, this way if ($query != false) { $result =$query; } else{ $result = "Error in numrows"; } return $result ; I've noticed a lot of people do that, any reason why, and weren't you the person complaining about longer processing.. that'd take longer wouldn't it? yes but i guess it is more handy when the result is in the variable Quote Link to comment https://forums.phpfreaks.com/topic/66812-solved-will-this-work-wrapped-function-for-numrows/#findComment-334942 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.