Julius Posted November 6, 2011 Share Posted November 6, 2011 Hello, I'm trying to make my first database class. I thought I will create a function for select, delete, update and insert, but then I thought, what if I'm going to use join while selecting something? and then I realised, that I need only one function, called query. Now, the issue is that I don't know how to make function know that I'm selecting some data from database, or I'm updating data. If I select data, I want my result variable to be filled with results. I hope you understand my question-problem. I want this to be smart, so if you have any ideas - please share. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/250558-check-for-mysql-results/ Share on other sites More sharing options...
haku Posted November 6, 2011 Share Posted November 6, 2011 I understand what you are saying, but I'm not sure what exactly you are having troubles with. Quote Link to comment https://forums.phpfreaks.com/topic/250558-check-for-mysql-results/#findComment-1285514 Share on other sites More sharing options...
PFMaBiSmAd Posted November 6, 2011 Share Posted November 6, 2011 A function/method that executes a query should only return the true (insert/update queries), false (any query that failed due to an error), or instance of a result class (select/show queries) from the query that was executed, nothing more. It is your main code that calls the query function/method that is aware of what type of query was being executed and knows what to test and to do with the returned true, false, instance of a result class. A result class would have functions/methods that allow you to actually fetch the data that a successful select/show query returns. Quote Link to comment https://forums.phpfreaks.com/topic/250558-check-for-mysql-results/#findComment-1285519 Share on other sites More sharing options...
Julius Posted November 6, 2011 Author Share Posted November 6, 2011 I'm pretty new in php oop and mysql. I don't know how to make my function so it could see the difference between select and insert, update, delete queries. <?php class DB { public $results = array ( ); public function query ( $query ) { $qr = mysql_query ( $query ); // now, if this was select query, i want to store results in $this -> results // otherwise do nothing or return true if query succeeded. } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/250558-check-for-mysql-results/#findComment-1285520 Share on other sites More sharing options...
PFMaBiSmAd Posted November 6, 2011 Share Posted November 6, 2011 All your code needs to do is TEST for a true, false, or result resource from the mysql_query() statement. It doesn't care or need to know what type of query was executed. Edit: see is_resource to test if a resource was returned. If a resource was not returned, then you got a true or a false value and you can just return that value. Quote Link to comment https://forums.phpfreaks.com/topic/250558-check-for-mysql-results/#findComment-1285522 Share on other sites More sharing options...
Julius Posted November 6, 2011 Author Share Posted November 6, 2011 BINGO! Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/250558-check-for-mysql-results/#findComment-1285527 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.