PHP PDO Truncate table

Hi guys. I have following code to delete particular row from a table based on the title. But how can I delete all data from the table?

[php]<?php
require ‘connpdo.php’;
$naslov = $_POST[‘naslov’];
#DELETE DATA
//delete some data
$sqlInsert = ‘DELETE FROM bloging WHERE title=:title’;
$preparedStatement = $conn->prepare($sqlInsert);
$preparedStatement->execute(array(’:title’ => $naslov));

//REDIRECT TO HOME PAGE
header(‘Location: http://localhost/ITAPHP/index.php’);
?>[/php]

just remove the condition.

I did wanna do that. But I don’t understand what to put in the last statement? I tried something like this:

[php]//delete some data
$sqlInsert = ‘DELETE FROM bloging’;
$sqlInsert->execute();[/php]

I know it’s wrong. I’ve got working version with mysql but need it with pdo. Can you show me code please.

Thank you!

this works fine here

[php]$bdd = new PDO(‘mysql:dbname=database_name;host=localhost;charset=utf8’, ‘database_user’, ‘database_pass’);
$result = $bdd->query(‘DELETE FROM table’);[/php]

It’s so simple. Thank you. It works fine. Thank you so much.

Cheers

wouldn’t this be faster?

[php]$bdd = new PDO(‘mysql:dbname=database_name;host=localhost;charset=utf8’, ‘database_user’, ‘database_pass’);
$result = $bdd->query(’‘TRUNCATE TABLE table’’);[/php]

Truncate is faster but there are differences. Two things, truncate will reset your auto-increment back to zero, delete continues counting and rollbacks are not possible with truncate. There are also issues with foreign keys. See attached image for differences.


truncate.png

Sponsor our Newsletter | Privacy Policy | Terms of Service