TechnoDiver Posted July 23, 2021 Share Posted July 23, 2021 I've been scrolling through some 3rd party code trying to get ideas and a deeper understanding of PHP. I came across these 2 simple methods in a class entitled 'Category' -> public function deleteCategory($id) { $query = mysqli_query($this->conn, "DELETE FROM top_categories WHERE top_cat_id=$id"); if($query) { return true; } else { return false; } } public function updateCategory($id, $category) { $query = mysqli_query($this->conn, "UPDATE top_categories SET top_cat_title='$category' WHERE top_cat_id=$id"); if($query) { return true; } else { return false; } } They're simple enough and work perfectly in the context of their functionality, but I don't understand how. Their instantiation and calls are ordinary but I don't understand how they do what they do. To me (a very untrained eye) they look like they initialize a variable ($query) and then check if it's initialized or not without actually doing anything with it. They've both been instantiated in a file that is included at the top of each page and the method calls are seemingly normal -> if(isset($_POST['edit_cat'])) { $cat_obj->updateCategory($cat_id, $_POST['cat_title']); header("Location: category.php?message=category-updated"); } if(isset($_GET['cat_id'])) { $cat_obj->deleteCategory($_GET['cat_id']); header("Location: category.php?message=deleted-successfully"); } Can someone with the time and will please explain how these 2 methods do the things they do? Quote Link to comment https://forums.phpfreaks.com/topic/313420-how-do-these-methods-work/ Share on other sites More sharing options...
kicken Posted July 23, 2021 Share Posted July 23, 2021 12 minutes ago, TechnoDiver said: Their instantiation and calls are ordinary but I don't understand how they do what they do. They execute a SQL Query by calling the mysqli_query function. The variable holds the result of whether that query was executed successfully or not (true/false). The variable and if/else statements are unnecessary, they could have just returned the result of mysqli_query directly instead. Quote Link to comment https://forums.phpfreaks.com/topic/313420-how-do-these-methods-work/#findComment-1588524 Share on other sites More sharing options...
TechnoDiver Posted July 23, 2021 Author Share Posted July 23, 2021 Those methods actually execute the query?! I feel dumb, it looks to me that $query is just being assigned a mysql query string to be used later, but yea, I get it now When you say that they could just return the result do you mean something like this?? -> $result = mysqli_fetch_array($query); return $result; If they would have just returned the result, as in my sample code directly above, I would have completely understood it as my confusion came from $query not being used again in the method Thanks for clarifying, it still doesn't feel right but I'll just get use to it lol Quote Link to comment https://forums.phpfreaks.com/topic/313420-how-do-these-methods-work/#findComment-1588528 Share on other sites More sharing options...
kicken Posted July 23, 2021 Share Posted July 23, 2021 UPDATE / DELETE queries don't have a result set, as such mysqli_query will just return true/false to indicate whether the query was successful or not. There's nothing to fetch so no reason to use mysqli_fetch_array. By returning the result, I mean those methods could have just been: public function deleteCategory($id) { return mysqli_query($this->conn, "DELETE FROM top_categories WHERE top_cat_id=$id"); } mysqli_query will return true or false. Adding the variable and if branch to check if it's true, just to then return true if it is, is entirely unnecessary. 1 Quote Link to comment https://forums.phpfreaks.com/topic/313420-how-do-these-methods-work/#findComment-1588542 Share on other sites More sharing options...
TechnoDiver Posted July 27, 2021 Author Share Posted July 27, 2021 Yea, that makes total sense now, I get it. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/313420-how-do-these-methods-work/#findComment-1588626 Share on other sites More sharing options...
maxxd Posted July 27, 2021 Share Posted July 27, 2021 Also, both methods should be using prepared statements - the delete is being driven off a $_GET variable. Not sure what the overall code base is that this came from, but I'd recommend not trusting anything you see in there all that much... Quote Link to comment https://forums.phpfreaks.com/topic/313420-how-do-these-methods-work/#findComment-1588635 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.