Re: FW: Can you help?
| From: | AllenJB | Date: | Tue, 29 Sep 2026 16:03:06 +0000 |
| Subject: | Re: FW: Can you help? | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-328870@lists.php.net to get a copy of this message | ||
On 2026-09-29 16:26, Chris Bas wrote:
*From: *Chris Bas <chrisbas4u@outlook.com>
*Date: *Tuesday, 29 September 2026 at 16:49
*To: *php-general@lists.php.net <php-general@lists.php.net>
*Subject: *Can you help?
Good day to you,
I allow myself to disturb you.
I try for long time to use PHP to truncate a table , please find below my script:
<!DOCTYPE HTML>
<html lang="en">
<head><meta charset="UTF-8">
<title>TRUNCATE with PHP</title>
</head>
<body>
<?php
$host = "localhost";
$user = “bill";
$mdp = “dogdog";
$base = "site_db";
$table = “fruit";
// Create connection
$conn = new mysqli($host, $user, $mdp, $base);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully!";
function empty_table($dbc)
{
$q = "TRUNCATE TABLE fruit";
$r = mysqli_query($dbc, $q);
if(mysqli_query($r))
// SUCCESS
echo " table “.$fruit." made empty !";
else
// FAILURE
echo "table “.$fruit." was not empty.";
}
// WE EMPTY
empty_table($table);
?>
</body>
</html>
In Localhost I receive this result:
Connected successfully!
*Fatal error*: Uncaught TypeError: mysqli_query(): Argument #1 ($mysql) must be of type mysqli, string given in /Users/chris/Applications/Abyss Web Server/htdocs/empty.php:28 Stack trace: #0 /Users/chris/Applications/Abyss Web Server/htdocs/empty.php(28): mysqli_query() #1 /Users/chris/Applications/Abyss Web Server/htdocs/empty.php(37): empty_table() #2 {main} thrown in */Users/chris/Applications/Abyss Web Server/htdocs/empty.php* on line *28*
Line 28 being: $r = mysqli_query($dbc, $q);
Well sorry to force myself but can you help!
Many thanks
Chris
Hi,
Your empty_table function is expecting the parameter passed to it to be the database connection handle ($dbc, a mysqli object), but you pass it a table name (then hard-code the table name in the function)
Additionally, mysqli_query() will always return a success response (assuming there's no MySQL error) regardless of what was in the table because TRUNCATE operations don't return any information about whether any rows were deleted or not ( https://dev.mysql.com/doc/refman/8.0/en/truncate-table.html - search for "affected")