I am trying to convert mysqli code to PDO way but I run into an issue.
It should behave: If deleted main parent delete his sub categories also, so that there are no "orphans" left in database.
The main parents are parent 0 in database, while sub categories are linked with parents ID. This is working mysqli example :
// Delete
if(isset($_GET['delete']) && !empty($_GET['delete'])) {
$delete_id = (int)$_GET['delete'];
$delete_id = sanitize($delete_id);
/* Deleting a parent and its children to avoid orphaned categories in the database. */
$result = $db->query("SELECT * FROM categories WHERE id = '{$delete_id}'");
$category = mysqli_fetch_assoc($result);
if($category['parent'] == 0) {
$db->query("DELETE FROM categories WHERE parent = '{$delete_id}'");
header("Location: categories.php");
}
$db->query("DELETE FROM categories WHERE id = '{$delete_id}'");
header("Location: categories.php");
}
What I have tried with PDO way:
//delete Category
if(isset($_GET['delete']) && !empty($_GET['delete'])){
$delete_id = (int)$_GET['delete'];
$delete_id = sanitize($delete_id);
//Deleting sub-categories if parent is deleted
$sql= $veza->prepare ("SELECT * FROM categories WHERE id = '$delete_id'");
$result = $sql->execute();
$category = $result->fetch(PDO::FETCH_ASSOC);
if($category['parent'] == 0){
$sql = "DELETE FROM categories WHERE parent = '$delete_id'";
$sql->execute();
}
$dsql=$veza->prepare("DELETE FROM categories WHERE id = '$delete_id'");
$dsql->execute($_GET);
header("location: categories.php");
}
I can't find the solution.
I have Uncaught Error: Call to a member function fetch() on boolean