Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php code to delete all records...should it be working

Tags:

php

Should this delete all records in my SQL table?

<?php
$con = mysql_connect("localhost","bikemap","pedalhard");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("test", $con);

mysql_query("DELETE * FROM gpsdata");

mysql_close($con);
?>
like image 675
Loren Zimmer Avatar asked Nov 11 '11 19:11

Loren Zimmer


People also ask

How do you delete a record from a table in PHP?

Delete Data From a MySQL Table Using MySQLi and PDO. Notice the WHERE clause in the DELETE syntax: The WHERE clause specifies which record or records that should be deleted. If you omit the WHERE clause, all records will be deleted!

What is the delete () in PHP?

There is no delete() function in PHP. If you need to delete a file, look at the unlink() function.


3 Answers

The DELETE syntax doesn't allow a star between DELETE and FROM. Try this instead:

mysql_query("DELETE FROM gpsdata");
like image 132
Mark Byers Avatar answered Sep 21 '22 23:09

Mark Byers


You might want to check out MySQL's Truncate command. That should remove all records easily.

like image 27
ford Avatar answered Sep 22 '22 23:09

ford


DELETE differs from Truncate. But if your use case is simple, go to any one.

Truncate

  • DDL command
  • You can't rollback
  • Table Structure will be re-created.
  • Your indexes will be lost.
  • Will delete all rows.

DELETE

  • DML command
  • You can rollback.
  • Structure remains as it is.
  • You can specify a range of rows to delete.
like image 27
Laith Shadeed Avatar answered Sep 21 '22 23:09

Laith Shadeed