Web Design Forums

Welcome! Please register or log in: Forgot your password? Why register?
You are here: Web Design Forums » Programming Help » PHP » strange php variable to mysql problem RSS

strange php variable to mysql problem

This thread was started by teknicalissue and has been viewed 688 times, and contains 7 replies, with the last reply made by Danny[MLWA].
Post Reply
1
View teknicalissue's reputation
Posted May 18 '09 at 09:20 PM
      Posts: 13
in my block of code
class DeleteClass{
    public function 
deleteSomething($item2,$uniqueField2,$table2){

        
$query mysql_query("DELETE FROM '$table2' WHERE '$uniqueField2' = '$item2'");
  if(!
query){
      echo 
mysql_error();
  }
}


this returns an error... but if i remove the single quotes from the php variables in the query.. it works.. why??? i thought you were supposed to add single quotes to php variables in querys?

Advertisement Register for free to hide these ads and participate in discussions!

2
1,251 points at 99% Moderator Repute
mlseim, WDF Moderator Private message  
Posted May 19 '09 at 08:59 AM
      Posts: 3,095
Don't be afraid to use Google for questions like these:
http://www.google.com/search?hl=en&q...ysql+singl e+
If it's zero degrees outside today, and it's supposed to be twice as cold tomorrow, how cold is it going to be?
mlseim is online now! Reply

3
50 points at 100%
curare, WDF User Private message  
Posted May 19 '09 at 01:04 PM
      Posts: 48
That is because, the $table2 variable should be backticked. So your code becomes:
class DeleteClass{
    public function 
deleteSomething($item2,$uniqueField2,$table2){

        
$query mysql_query("DELETE FROM `$table2` WHERE '$uniqueField2' = '$item2'");
  if(!
query){
      echo 
mysql_error();
  }
}


The backtick is the character under the tilde (~) on most keyboards.

4
View teknicalissue's reputation
Posted May 19 '09 at 02:31 PM
      Posts: 13

i did look it up in google.. i couldn't find it thus i came here.. thank you for the link though.

now that im here, why does the table name need to be backticked? (i can look this up on google but now that im getting replies i would like a more in depth explanation)

5
50 points at 100%
curare, WDF User Private message  
Posted May 19 '09 at 02:49 PM
      Posts: 48
I believe it's to differentiate it from a field, but there is probably more to it that just that.

6
424 points at 99% Repute WDFplus Member
Eddy Bones, ColdFusion Programmer Home page   Private message  
Posted May 19 '09 at 03:29 PM
      Posts: 1,054
Table and field names technically do not need to be quoted or backticked. I'm not aware of any standard that is met by doing so. However, I have often seen table names and such backticked. Maybe I'm just a lazy programmer

As far as I know, PHP does not parse variables that are wrapped in single quotes. If you wanted to use them you'd have to start concatenating the query string like this:
$query mysql_query("DELETE FROM '".$table2."' WHERE '".$uniqueField2."' = '".$item2."'"); 

That gets pretty crazy... I suppose that's a good reason why backticks can be used, since single quotes are such a widely-used syntactical element in many languages and will potentially blow things up.

7
View teknicalissue's reputation
Posted May 20 '09 at 02:00 PM
      Posts: 13
ok thank you, im still a tad bit confused though i got my information from this mysql post http://forums.mysql.com/read.php?52,...437#msg-247437
and ive been using this technique for months now. this is the first time where this has failed lol if this is wrong then why does it work? if its not then why doesn't it work for this specific instance?. while im at it, can anyone explain %S for me? lol i look it up in google and it completly ignores my query.

8
32 points at 100%
Posted June 10 '09 at 05:17 AM
      Posts: 76
Absolute best way to stop hackers dead in their tracks, is to make sure that ANY and ALL input fields are protected with:

$post_this_val_instead = mysql_real_escape_string($_POST['some_field_name']);

Wham, all injections halted =)

http://us3.php.net/manual/en/functio...ape-string.php

in my block of code
class DeleteClass{
    public function 
deleteSomething($item2,$uniqueField2,$table2){

        
$query mysql_query("DELETE FROM '$table2' WHERE '$uniqueField2' = '$item2'");
  if(!
query){
      echo 
mysql_error();
  }
}


this returns an error... but if i remove the single quotes from the php variables in the query.. it works.. why??? i thought you were supposed to add single quotes to php variables in querys?

instead of using 's use `s instead...

class DeleteClass{
    public function 
deleteSomething($item2,$uniqueField2,$table2){

        
$query mysql_query("DELETE FROM `" $table2 "` WHERE `" $uniqueField2 "` = `" $item2 "`");
  if(!
query){
      echo 
mysql_error();
  }
}



Also I broke the query up into a breaking string for you as well with the quotes and periods. When you use "'s in a query like that you're telling php to try and detect any variables in that string... Sometimes there aren't any and sometimes I just can't find them or thinks that it is not a variable where there is not a space before the variable... so '$variable would be skipped as a variable... Where as ' $variable would work. The safest way to do it is like so:

"SELECT * FROM `" . $tablename . "` WHERE....."
Last edited June 10 '09 at 06:02 AM by Wired ("merged posts"). Reply

Post Reply

Similar Threads
Thread Thread Starter Forum Replies Last Post
Top 21 PHP Programming mistakes thexchord PHP 18 Yesterday 09:52 AM
Help: expand/collapse menu with php and mysql ChiDao PHP 1 February 2 '06 01:54 PM
Getting apache, mySQL, and PHP to run.. web_designers Linux, Apache, MySQL, PHP (LAMP) Server Help 0 June 28 '05 01:22 PM
Php print & highlighting problem japaja PHP 0 April 14 '03 02:34 PM
Basic PHP Uploads Tutorial thexchord Coding Articles & Tutorials 2 May 2 '02 09:28 PM