|
fletcher posted:Or instead of reinventing the wheel you use PDO so other people that read your code don't think you are crazy. PDO is nice and all, but you still need to write a half-dozen lines of code to do anything useful. I use a set of sprintf-like wrapper functions that automatically escape arguments, run the query, and return the first field of the first result, or the first result row as an array, or the result set, etc.
|
# ¿ Aug 31, 2010 00:45 |
|
|
# ¿ May 21, 2024 01:25 |
|
fletcher posted:While those 6 lines are such a huge burden to type out (are you kidding me?) they are waaaaaay easier to read and debug. There's no way that code:
|
# ¿ Aug 31, 2010 01:07 |
|
fletcher posted:If all you ever have to write are the simplest queries conceivable, that may be sufficient. Unfortunately the real world does not work this way. Utility functions like that do have their place once in awhile, but in my experience it's fairly limited. Those are things that have to be done every time you want one field. It doesn't help anyone to see them done yet again; why not made your code more concise without sacrificing readability by hiding them in a method that does it for you? Just because the query gets more complex doesn't mean the scaffolding you have to put around it to get useful data back does- most of the time you want a field, or a row, or a result set back, and helper functions make it easy on you. MrMoo posted:You should be using prepared statements, even better use stored procedures. Stored procedures are nice for when you have one query you use in a lot of different places, but at my job that's the case for exactly one query that I can think of, and I can't even imagine what a royal pain it would be to store several thousand queries and update our codebase accordingly.
|
# ¿ Aug 31, 2010 02:35 |
|
VerySolidSnake posted:I'm trying to make an automated installer / configuration utility for a framework and I've hit a snag. The configuration file is one giant array. Creating the array is no problem, however creating a file in it's place (using something like fwrite) with that array in a usable form has be stuck. Any ideas? Serialization is unfortunately not an option. Are you looking for something like var_export()?
|
# ¿ Nov 23, 2011 17:30 |
|
the php manual posted:var_export() gets structured information about the given variable. It is similar to var_dump() with one exception: the returned representation is valid PHP code.
|
# ¿ Nov 23, 2011 17:43 |
|
OriginalPseudonym posted:Is there any functional difference between var_export and serialize()? Just asking out of curiousity, since the latter is what I typically go to for stuff like this. serialize() returns a string that can be unserialize()'d later to get the same value. var_export() doesn't need an unserializing function because it returns actual PHP code, and you use it when you want to do something like have a PHP script that generates another PHP script. Using either function can be a sign that you're about to do something gross, especially if you're sticking a serialized object into a database or using var_export() in tandem with eval().
|
# ¿ Nov 23, 2011 21:11 |
|
the_cow_fan posted:Whats the best framework to use nowadays? a lot of people seem to be against cake for various reasons, I've been mainly looking at symfony. It's hard to say what the "best framework" is. Asking people that question is probably going to get similar results as asking "What framework are you most familiar with?" Give some thought to whether you really need a framework. PHP is actually pretty good on its own, and its frameworks tend to be big, lumbering, heavyweight things that force you into using their style. Rasmus Lerdorf (the dude who created PHP) is often critical of the larger frameworks, and wrote this article on a "no-framework MVC framework" to illustrate how you can write clean, well-organized code without using an external framework. Personally, when I do need to use a framework, I like the Zend Framework. It's pretty good about letting you use only the parts you need. e: apparently that link is down, here's google's cache in the meantime
|
# ¿ Nov 29, 2011 20:11 |
|
Sure, if you're new to PHP and/or you're doing something that's already been done to death and/or performance isn't a concern, there's nothing wrong with using a framework. I'm just saying that a lot of developers consider copying over their MVC framework of choice to be the immutable step 0 of every project, but using a framework and using an MVC architecture are not only not prerequisites to writing good code but sometimes actively get in the way of getting things done.VerySolidSnake posted:edit: Also, didn't Rasmus argue against Object Oriented code at one point, and instead wrote a whole blog post about the benefits of procedural instead? He really isn't a good source of information. I don't ever remember reading this, but I'm curious as to why you think it would discredit him. OOP isn't the one true way of doing things, it's just a paradigm with its own set of issues.
|
# ¿ Nov 29, 2011 21:33 |
|
Experto Crede posted:At work, I run a unix command using watch to view the queue on our PBX, ideally I'd like to find a way to show this in a browser also in real time. You can use popen() instead of shell_exec(), which will give you a stream you can read from just like a file.
|
# ¿ Oct 22, 2014 22:08 |
|
Does anyone know a way to get the SSH2 functions to connect via a proxy? Or any SSH library that will let me connect via a proxy? The only other thing I can think of doing is using proc_open('proxychains ssh...') but I really don't want to wrangle the input/output pipes if I can help it.
|
# ¿ Oct 22, 2014 22:29 |
|
Laravel is about to make me lose my poo poo. I've got these two Eloquent models, Thing and ThingBag. Thing has a BelongsTo relationship with ThingBag, and ThingBag has a HasMany relationship with Thing. So I iterate over the Things in the ThingBag and do some work with them. I may need to remove a Thing from the ThingBag, which I do with: code:
code:
This has got to be a common enough problem that I've just overlooked something in Laravel's vast, comprehensive documentation, right?
|
# ¿ Jan 8, 2016 23:33 |
|
Depressing Box posted:You can call $thingbag->load('things') to refresh the model's things relationship from the database. Yeah, but I'd prefer not to hit the database again for something I already know.
|
# ¿ Jan 9, 2016 04:51 |
|
Depressing Box posted:Ok, then instead of assigning directly to $thingbag->things, try: That's exactly what I needed, thanks a lot.
|
# ¿ Jan 9, 2016 06:50 |
|
|
# ¿ May 21, 2024 01:25 |
|
That's a hideous table structure. Really bad. Anyway here's a rough idea of how to do what you want, assuming that each of your 136 attribute columns default to null and don't need to be specified unless they have a value. Good luck! php:<?php foreach ($json_documents as $document) { $row = [ 'item_id' => $document['item_id'], 'name' => $document['name'], // set other top-level elements ]; $attr_count = 1; foreach ($document['attributes'] as $attr) { $row["attribute{$attr_count}_id"] = $attr['attribute_id']; $row["attribute{$attr_count}_value"] = $attr['value']; $attr_count++; } do_insert($row); }
|
# ¿ Jul 6, 2022 02:25 |