0

I'm following a course about php and mysql and have a task about CRUD. When i execute a query to delete the contents of a table and related tables everything works but i get:

Fatal error: Uncaught Error: Call to a member function close() on bool in /Applications/XAMPP/xamppfiles/htdocs/LOI/hoofdstuk10/opgave/schoon.php:10 Stack trace: #0 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/LOI/hoofdstuk10/opgave/schoon.php on line 10.

What could be wrong? It's a very simple document so perhaps it's in the sql document.

<?php

require_once 'login.php';

$query = "DELETE FROM Lid";

$result = $conn->query($query);
if (!$result) die ('DELETE failed');

$result->close();
$conn->close();
?>

SQL

CREATE TABLE Postcodes (
    postcode CHAR(6) NOT NULL,
    straat VARCHAR(60) NOT NULL,
    woonplaats VARCHAR(60) NOT NULL,
    PRIMARY KEY (postcode));

CREATE TABLE Lid (
    lidnummer INT NOT NULL AUTO_INCREMENT,
    naam VARCHAR(30) NOT NULL,
    voornaam VARCHAR(30) NOT NULL,
    postcode CHAR(6) NOT NULL,
    huisnummer VARCHAR(10) NOT NULL,
    PRIMARY KEY (lidnummer),
    CONSTRAINT FK_postcode FOREIGN KEY (postcode)
    REFERENCES Postcodes(postcode));

CREATE TABLE email (
    lidnummer INT NOT NULL,
    emailadres VARCHAR(60),
    PRIMARY KEY (emailadres),
    CONSTRAINT FK_email FOREIGN KEY (lidnummer)
    REFERENCES Lid(lidnummer) ON DELETE CASCADE);

CREATE TABLE Telefoonnummers (
    lidnummer INT NOT NULL,
    telefoonnummer VARCHAR(60),
    PRIMARY KEY (telefoonnummer),
    CONSTRAINT FK_telefoonnummer FOREIGN KEY (lidnummer)
    REFERENCES Lid(lidnummer) ON DELETE CASCADE);


INSERT INTO Postcodes(postcode, straat, woonplaats)
    VALUES('3333AA', 'van Goghsingel', 'Amsterdam'),
            ('4444BB', 'Rembrandtlaan', 'Rotterdam'),
            ('5555CC', 'Mondriaanstraat', 'Den Haag');

INSERT INTO Lid(lidnummer, naam, voornaam, postcode, huisnummer)
    VALUES(1, 'Jansen', 'Jan', '3333AA', '10'),
            (2, 'Pietersen', 'Piet', '4444BB', '20'),
            (3, 'Klaassen', 'Klaas', '5555CC', '30');

INSERT INTO email(lidnummer, emailadres)
    VALUES((SELECT lidnummer FROM Lid WHERE postcode = '3333AA' AND huisnummer = '10'), 'jan@gmail.com'),
            ((SELECT lidnummer FROM Lid WHERE postcode = '4444BB' AND huisnummer = '20'), 'piet@gmail.com'),
            ((SELECT lidnummer FROM Lid WHERE postcode = '5555CC' AND huisnummer = '30'), 'klaas@gmail.com');

INSERT INTO Telefoonnummers(lidnummer, telefoonnummer)
    VALUES((SELECT lidnummer FROM Lid WHERE postcode = '3333AA' AND huisnummer = '10'), '0655554444'),
            ((SELECT lidnummer FROM Lid WHERE postcode = '4444BB' AND huisnummer = '20'), '0632323232'),
            ((SELECT lidnummer FROM Lid WHERE postcode = '5555CC' AND huisnummer = '30'), '0612345678');
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • @mickmackusa Did you read the question? This dupe close makes no sense. – Paul Spiegel Jun 02 '20 at 14:56
  • @Paul I didn't notice that the OP was trying free a result set that never was. My perspective was that the conditional `die()` was inaccurate -- that the checking for syntax errors was only half of the indicator that there was successful deletion. – mickmackusa Jun 02 '20 at 15:26

1 Answers1

1

mysqli query returns true;

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

Your request returns no data, that's why you receive true.

Just delete this row:

$result->close();
Max Shaian
  • 418
  • 4
  • 11
  • @NigelRen. This line checks the error of the request in case it returns false. In the code above, it works just fine and returns true. But it's still a boolean value. There is no need to close something on it. – Max Shaian Jun 02 '20 at 08:32