PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 21st, 2013, 08:01 AM
Northie's Avatar
Northie Northie is offline
Square Peg in a Round Hole
Click here for more information.
 
Join Date: Oct 2007
Location: North Yorkshire, UK
Posts: 3,419 Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 10 h 15 m 39 sec
Reputation Power: 3896
Pseudo Prepared Statements for mysql_

There's been a lot of talk recently about the older mysql_* functions within PHP.

The main gripe from those telling others not to use it is down to security.

I've dug out an old bit of code which attempts to emulate prepared statements for the mysql_query fuction:

php Code:
Original - php Code
  1.  
  2. <?php
  3.  
  4. function mysql_prepared_query ($sql,$args=false,$con=false) {
  5.     if($args) {
  6.         if(is_array($args)) {
  7.             $find = $replace = array();
  8.             foreach($args as $key => $val) {
  9.                 $find[] = ":".$key;
  10.                 if($con) {
  11.                     $replace[] = "'".mysql_real_escape_string($val,$con)."'";
  12.                 } else {
  13.                     $replace[] = "'".mysql_real_escape_string($val)."'";
  14.                 }
  15.             }
  16.            
  17.             $sql = str_replace($find,$replace,$sql);
  18.            
  19.         } else {
  20.             throw new Exception('If $args is supplied then it must be an array, '.gettype($args).' supplied');
  21.         }
  22.     }
  23.    
  24.     //for debugging, comment out in production
  25.     return $sql;
  26.    
  27.     if($con) {
  28.         return mysql_query($sql,$con);
  29.     }
  30.    
  31.     return mysql_query($sql);
  32. }
  33.  
  34. $sql = "
  35.     SELECT
  36.         *
  37.     FROM
  38.         `table`
  39.     WHERE
  40.         `name` LIKE :name;
  41.         OR
  42.         `class` = :class
  43.     ;
  44. ";
  45.  
  46. $args['name'] = '%Northie%';
  47. $args['class'] = 'User';
  48.  
  49. //debug
  50. echo mysql_prepared_query($sql,$args);
  51.  
  52. $sql = "
  53.     SELECT
  54.         *
  55.     FROM
  56.         `table`
  57.     WHERE
  58.         `name` = :name;
  59.     ;
  60. ";
  61. //debug
  62. echo mysql_prepared_query($sql,'Northie');
__________________
PHP OOPS! <?php DB::Execute(SQL::makeFrom($_GET))->fetchArray()->FormatWith(Template::getInstance('default'))->printHtml(); ?>

PDO vs mysql_* functions: Find a Migration Guide Here

[ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]

Reply With Quote
  #2  
Old January 21st, 2013, 11:04 AM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,863 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 19 h 44 m 40 sec
Reputation Power: 813
Hi,

well, I don't really see the point of "emulating" prepared statements. I mean, if people are willing to rewrite their whole database code, they should rather choose "the real thing" and use PDO/MySQLi. Why should they take this intermediate step just to replace everything again in a few years?

Reply With Quote
  #3  
Old January 21st, 2013, 11:19 AM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,698 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 4 h 53 m
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
Adding a new function and trying to use it in new/touched code doesn't sound like "rewriting their whole database code" to me.
It's not always fair to ask people to redo everything: there are a number of reasons why that might not be an option. If functions like this get them one step closer to doing it, or even if not but they realize this is more secure than what they have now, then I say it's worth it.

Reply With Quote
  #4  
Old January 21st, 2013, 05:30 PM
E-Oreo's Avatar
E-Oreo E-Oreo is offline
Lost in code
Click here for more information.
 
Join Date: Dec 2004
Posts: 7,931 E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
Time spent in forums: 2 Months 7 h 48 m 54 sec
Reputation Power: 7053
It's a useful little function, no doubt, but if the goal is to ease the transition into PDO or MySQLi it seems like it would be better build an interface that matches the API used by PDO / MySQLi - ie with prepare and execute. That way if they ever do actually decide to fully convert to the superior interfaces they won't have to rewrite as much code using your compatibility layer.

Also it's worth noting that the behavior of this function is not fully compatible with real prepared statements. It serves the same purpose as far as security goes, but this function allows things that normal prepared statement would not.

I think a bigger problem with the MySQL extension is that it's deprecated and slow. It's not actually that difficult to write secure code using them, as you've demonstrated.
Comments on this post
ManiacDan agrees: "I think a bigger problem with the MySQL extension is that it's deprecated and slow" &lt;-- that
__________________
PHP FAQ
How to program a basic, secure login system using PHP

Quote:
Originally Posted by Spad
Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Pseudo Prepared Statements for mysql_

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap