I want to clear all the entries from one table in MySQL with php I tried this:
<?php
// Create connection
$con=mysqli_connect("localhost","username","password","dbName");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "TRUNCATE TABLE tableName";
mysqli_query($sql);
?>
but it didn't work. why?
The statement SQL DELETE ALL ROWS is used to delete all rows from the table.
Its basic syntax is as follows: SELECT column1_name, column2_name, columnN_name FROM table_name; Let's make a SQL query using the SELECT statement, after that we will execute this SQL query through passing it to the PHP mysqli_query() function to retrieve the table data.
The DROP TABLE statement is used to drop an existing table in a database.
This is a typo. You used mysql_query()
instead of mysqli_query()
. Change
mysql_query($sql);
to:
mysqli_query($con, $sql);
Also note that the param lists of both functions differ. mysqli_expects()
a connection handle as it's first param.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With