Web Design Forums

PHP

Have questions about PHP? Ask them here and our experts will assist you before you know it! You can also find help in the documentation at PHP.net.

strange php variable to mysql problem



Site of the Month Nominations
ENTER YOUR SITE NOW!

Reply
 
LinkBack Thread Tools
Old May 18 '09, 08:20 PM (#1)
teknicalissue is offline
New Member!
 
teknicalissue's Avatar
 
Join Date: November 2008
Posts: 13
teknicalissue is an unknown quantity at this point
strange php variable to mysql problem

in my block of code
PHP 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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old May 19 '09, 07:59 AM (#2)
mlseim is offline
WDF Staff
 
mlseim's Avatar
 
Join Date: April 2004
Location: Cottage Grove, Minnesota
Posts: 3,401
mlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud of
Don't be afraid to use Google for questions like these:
http://www.google.com/search?hl=en&q...=mysql+single+
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old May 19 '09, 12:04 PM (#3)
curare is offline
WDF Member
 
curare's Avatar
 
Join Date: February 2009
Posts: 48
curare will become famous soon enough
That is because, the $table2 variable should be backticked. So your code becomes:
PHP Code:
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old May 19 '09, 01:31 PM (#4)
teknicalissue is offline
New Member!
 
teknicalissue's Avatar
 
Join Date: November 2008
Posts: 13
teknicalissue is an unknown quantity at this point
Quote:
Originally Posted by mlseim
Don't be afraid to use Google for questions like these:
http://www.google.com/search?hl=en&q...=mysql+single+
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)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old May 19 '09, 01:49 PM (#5)
curare is offline
WDF Member
 
curare's Avatar
 
Join Date: February 2009
Posts: 48
curare will become famous soon enough
I believe it's to differentiate it from a field, but there is probably more to it that just that.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old May 19 '09, 02:29 PM (#6)
Eddy Bones is offline
ColdFusion Programmer
 
Eddy Bones's Avatar
 
Join Date: January 2004
Location: Washington, USA
Posts: 1,054
Eddy Bones is just really niceEddy Bones is just really niceEddy Bones is just really niceEddy Bones is just really nice
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:
PHP Code:
$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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old May 20 '09, 01:00 PM (#7)
teknicalissue is offline
New Member!
 
teknicalissue's Avatar
 
Join Date: November 2008
Posts: 13
teknicalissue is an unknown quantity at this point
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old June 10 '09, 04:17 AM (#8)
Danny[MLWA] is offline
WDF Regular
 
Danny[MLWA]'s Avatar
 
Join Date: June 2009
Location: Goldsboro, North Carolina, USA
Posts: 109
Danny[MLWA] is on a distinguished road
Absolute best way to stop hackers dead in their tracks, is to make sure that ANY and ALL input fields are protected with:

Code:
$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

Quote:
Originally Posted by teknicalissue
in my block of code
PHP 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...

PHP Code:
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:

Code:
"SELECT * FROM `" . $tablename . "` WHERE....."

Last edited by Wired; June 10 '09 at 05:02 AM. Reason: merged posts
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

  Web Design Forums » Programming Help » PHP

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Top 21 PHP Programming mistakes thexchord PHP 19 June 18 '10 02:20 PM
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 Server Administration Help 0 June 28 '05 12:22 PM
Php print & highlighting problem japaja PHP 0 April 14 '03 01:34 PM
Basic PHP Uploads Tutorial thexchord Coding Articles & Tutorials 2 May 2 '02 08:28 PM

 
User Infomation
Your Avatar

Site Of The Month

Ticket Cake
Ticket Cake

Ticket Cake is a drupal based event ticketing platform. It features that ability to browse events and share them.

Nominate Your Site Now!

Advertisement
WolfCMS.org

Latest Articles
- by RickM
- by bfsog

Advertisement

Partner Links



All times are GMT -4. The time now is 02:33 AM.


WebDesignForums.net is Copyright © 2010 RikeMedia.

SEO by vBSEO

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163