Clear all records from table with php

I want to delete all records from a single table in MySQL using 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?

+6
source share
3 answers

This is a missprint. You used mysql_query() instead of mysqli_query() . The change

 mysql_query($sql); 

in

 mysqli_query($con, $sql); 

Also note that the parameter lists of both functions are different. mysqli_expects() handle to the connection as the first parameter.

+6
source

After creating a connection using "mysqli", you try to delete all entries in "dbName" using mysql_query.

Change the code to something like

 <?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($con, $sql) or die(mysqli_error()); ?> 

See if this works and let me know.

+2
source

First, check for error messages that may give the key; there are some restrictions that may prevent TRUNCATE from working. Also make sure this is not a typo with mysql / mysqli functions, as in your question.

If the table is not huge or performance is not critical, just try:

 $sql = "DELETE * FROM tableName"; 
-1
source

All Articles