Jump to content

How do these methods work?


TechnoDiver

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

  • Like 1
Link to comment
Share on other sites

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...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.