Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clear all the entries from a table with php

Tags:

php

mysql

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?

like image 283
AshKan Avatar asked Apr 11 '13 11:04

AshKan


People also ask

Which command is used to remove all rows from a table in PHP?

The statement SQL DELETE ALL ROWS is used to delete all rows from the table.

How can I select all records from a table in SQL in PHP?

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.

Which statement is used to delete a table?

The DROP TABLE statement is used to drop an existing table in a database.


1 Answers

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.

like image 136
hek2mgl Avatar answered Oct 06 '22 01:10

hek2mgl