PHP/mySQL noob question

alexxz4

New Member
Messages
21
Reaction score
0
Points
0
Hi,
This is my second day programming in PHP/mySQL. Im trying to delete an entry.
My table has three rows: name, val1, val2 and im trying to delete a row which has name 'stevenjones'
Code:
[B][U]function[/U] db_delete($nm) {
@mysql_query("DELETE FROM table WHERE name=$nm");
}
It says
[/B]Error: Unknown column 'stevenjones' in 'where clause' 0
 

DeadBattery

Community Support Team
Community Support
Messages
4,018
Reaction score
120
Points
0
I think the problem is you are trying to delete a column, not a row.
That is what I think, but I am not experienced with MySQL.
:)
 

alexxz4

New Member
Messages
21
Reaction score
0
Points
0
Ok, i got it, it supposed to say:
Code:
function db_delete($nm) {
@mysql_query("DELETE FROM table WHERE name='{$nm}'");
}
 
Last edited:

Adam01

New Member
Messages
114
Reaction score
0
Points
0
You dont need the {} around the variable.
Whats the @ for?
 

taekwondokid42

New Member
Messages
268
Reaction score
0
Points
0
When I'm trying to delete something, my syntax usually looks like:

//mysql_connection

mysql_query("DELETE FROM example WHERE id = $example_id");


I've never found that more is necessary.
 

DeadBattery

Community Support Team
Community Support
Messages
4,018
Reaction score
120
Points
0
What was the difference between the first and second pieces of code?
 

alexxz4

New Member
Messages
21
Reaction score
0
Points
0
You dont need the {} around the variable.
Whats the @ for?
Well i tried using it without {} or without single quotes and it didnt work.
@ supposed to block any errors returning from the sql call, ive read.
 

LHVWB

New Member
Messages
1,308
Reaction score
0
Points
0
The mysql_query() function requires the '$var' or {$var}, to make the variable work. Otherwise it will not be treated as a php variable. You should not need both of them at the same time.
 

cowctcat

New Member
Messages
401
Reaction score
0
Points
0
why dont you just go into phpmyadmin and delete the row there its as simple as clicking a button.
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
why dont you just go into phpmyadmin and delete the row there its as simple as clicking a button.

That hardly helps him with learning php/mysql. And what's more, it's completely illogical to have deletions done by a human when the script can take care of them on the spot.

The mysql_query() function requires the '$var' or {$var}, to make the variable work. Otherwise it will not be treated as a php variable. You should not need both of them at the same time.

Curly braces in a string (with a variable enclosed, of course) only really need to be used when accessing a value in an associative array, otherwise you'll get a parse error for trying to do that. For any other instance, their use is usually merely to help with clarification of the string. Meaning there should be no difference between "$var" and "{$var}".

For a query like the one in the OP, single quotes need to be put around the variable to specify to mysql that the value is a string. Otherwise it assumes you want the value in column 'name' compared to the value in column $nm, which could be any existent or nonexistent column.
 

expose

New Member
Messages
8
Reaction score
0
Points
0
Code:
[B]function db_delete($nm) {[/B]
[B]@mysql_query("DELETE FROM table WHERE name=$nm");[/B]
[B]}[/B]

Try this:
Code:
//open a database connection
$conn = mysql_connect("localhost", "mysql_username", "mysql_password");
mysql_select_db("db_name",$conn);
 
function db_delete($nm) {
 
$query_delete = "DELETE FROM `table` WHERE `name` = '$nm'";
$result = mysql_query($query_delete,$conn) or die(mysql_error()."<br /><br /><h3>An Error occured while trying to handle your request</h3>");
 
}

I always execute my queries using the above structure, as it helps cut down on redundant typing. Furthermore, you don't want to silence errors, you want them displayed in a friendly manner so users can contact you with them so that you can resolve them.
 
Top