1

Unfortunately, I wrote a lot of code using mysql_query, mysql_fetch_array and mysql_num_rows calls. It's close to finish, but today I've read about PDO, mysqli. The only hope I shouldn't start over and spend weeks is the answer of Well if you already have a lot of code written, I wouldn't bother, but for any new projects I would strictly suggest PDO.– musicfreak May 15 '09 at 5:23 The answer was 2.5 years ago, so the answer could be out of date as well. One of the reasons to switch to PDO is that it helps protect against SQL injections. I "clean" all inputed strings so I think that's not an argument for me. Better performance (if it's really better) is important as well, but the web-site works fine even now. Should I spend weeks to rewrite the code? Or that's not a big deal if it works? Sorry for this question, but I'm confused. Reading more documentation doesn't help me to choose a proper answer in my case. I'm not sure if I spend a lot of time I will get any better results or performance (alghout if I start it now, I would start with PDO as it is newer).

Thank you.

Community
  • 1
  • 1
Haradzieniec
  • 9,086
  • 31
  • 117
  • 212

2 Answers2

2

You don't have to worry about switching. If the project is almost ready, and you will start on a new one soon, just follow the new, better practices with your next project.

On the other hand, you just collected some useful experience. With a well-designed codebase, it should not be a lot of worry to switch between mysql_* and PDO. Write objects and methods to gather data, so your actual SQL calls will be quite few.

kapa
  • 77,694
  • 21
  • 158
  • 175
  • Thank you for your answer. You gave me a hope. Am I right: if I use only update, delete, select, insert and mysql_query, mysql_fetch_array and mysql_num_rows in my project, the only thing to change would be rewrite connect to MySQL db and follow the standard algorythms how to use PDO for Queries (spending like 2 minutes on 1 query), and no additional software is needed? I feel like I don't see many pitfalls before I start... I'm reading http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html , and that doesn't look so bad... until I started :D – Haradzieniec Nov 23 '11 at 08:31
  • @Haradzieniec Be sure to familiarize yourself with prepared statements, and that's all. – kapa Nov 23 '11 at 09:34
0

Just keep in mind that starting from PHP 5.4 (at the moment in RC state) the mysql extension is going to be "softly" deprecated in favour of mysqli or PDO (source). If your project is meant for the the long run and you think you're gonna switch to PHP 5.4+, or if you use a shared hosting and have no control over PHP version, you should probably consider slowly migrating (I'm doing that myself on a couple of projects), otherwise I wouldn't bother. But indeed, on new projects PDO is the way to go.

jamm3r
  • 55
  • 4
  • Thank you. Could you please explain me briefly how do you migrate so it doesn't take a whole life? Evey query request should be rewritten and I don't understand how people make it fast... – Haradzieniec Nov 27 '11 at 23:46
  • If there is a new version where a widely used feature (mysql_* in this case) will not be available, shared hostings will not switch for a long while. – kapa Nov 28 '11 at 09:07
  • @Haradzieniec People write objects, abstraction layers, etc. Most people using mysql_* just put the query and all the calls right into the code where they need it, which results in duplication and queries being all over the code. – kapa Nov 28 '11 at 09:16
  • @Haradzieniec I don't know how they make it fast either :D I'm not sure there's a way, since you actually have to rewrite your queries and change a bit the whole code (try/catch blocks, etc.). – jamm3r Nov 28 '11 at 09:24
  • @bazmegakapa indeed you're right, that's why I said "in the long run". I have a project supposed to work without much assistance for the next 3-4 years at least on a shared hosting, I'm rewriting that in PDO to make sure that it won't explode at a sudden version change :D – jamm3r Nov 28 '11 at 09:30
  • OK, guys... How to slowly migrate? Let's say, how to rewrite a part of php page (10% of queries on the page) so it all works? Should I use two different connections to the server? Or, I have to open another topic for this question?... Thank you. – Haradzieniec Nov 28 '11 at 09:35
  • @Haradzieniec Seems like you have to go the hard way... change all the queries by hand (you can open a question for that). But next time, you can better design your application. Use a framework, or at least analyze the code of some frameworks. – kapa Nov 28 '11 at 09:51