MySQL Delete Syntax

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
TomGeorge
Forum Newbie
Posts: 8
Joined: Fri Jan 17, 2003 5:56 am

MySQL Delete Syntax

Post by TomGeorge »

Hi again,

Was just wondering if anyone had any suggestions for the following code as it is not working.

$query = "DELETE FROM $daydel WHERE hours=$hours AND minutes=$minutes)";

There is no problem at all with the variables form what I can gather.

Your help would be much appreciated as always :-)

Thanks,

Tom - tl.george@virgin.net
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

remove the ) after $minutes

perform the query like

Code: Select all

$result = mysql_query($query, $conn) or die($query .' :'. mysql_error());
to find those errors
TomGeorge
Forum Newbie
Posts: 8
Joined: Fri Jan 17, 2003 5:56 am

Yup, that did it!

Post by TomGeorge »

Ok, yes that worked fine thanks! Didn't see that in there, that was a left over from a previous attempt! Finally, anyone got an idea as to why the following won't work?

$query = "DELETE FROM `$day`";

To empty a table.

Regards

TG
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

is there anything in mysql_error() ?
does $query contain what you expect ( echo $query; ) ?

you might also use "TRUNCATE tablename" but it will reset the auto-increment field to 0 (if there is).
TomGeorge
Forum Newbie
Posts: 8
Joined: Fri Jan 17, 2003 5:56 am

Post by TomGeorge »

Below is the code I am using. I keep getting the "Illegal Query 1" response. Also, I tried putting in the line which you suggested for debugging but it couldn't parse it.

<?php
if (!$link = mysql_connect("localhost", "EGKK", "sitelink"))
exit("Unable to make a connection to the database");

if (!mysql_select_db("EGKK", $link))
exit("Unable to select the database");

// Define a SQL Select Query
$query = "TRUNCATE $day ";
if (!$result = mysql_query($query, $link))
exit("Illegal query 1");
?>

Regards

TG
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

just use mysql_error() in your exit-statements.
http://www.php.net/manual/en/function.mysql-error.php
TomGeorge
Forum Newbie
Posts: 8
Joined: Fri Jan 17, 2003 5:56 am

Post by TomGeorge »

Ok it's working super now, thanks a lot everyone!

TG 8)
Post Reply