|
Adraeus posted:Why would a single table be a bad idea? Every time you do that, a DBA shoots themselves in the head. Unless you have a good reason (and 99.9% of the time you won't), don't do that. Anyone that has to maintain that will curse your name. Granted, it might not be a big deal in this one instance, but you shouldn't get in the habit of doing that, because it is very, very wrong. If you ever need to query on that field, you're hosed because you'll have to do string operations, which are slow as all hell on non-trivial data sets. You're using a relational database. Store your data as relations, don't do comma separated fields. Reference tables are your friend, and make whoever comes after you not hate you.
|
# ? Jan 23, 2013 16:14 |
|
|
# ? Jun 9, 2024 02:15 |
|
Everything that he said ^ At any time if you find yourself storing values into a single field with a delimiter, then 99.9% of the time, your database design is wrong and you need to rethink your strategy. You can't query off it like the above poster mentioned without doing slow string functions. Let's say you wanted to count how many websites a user has. code:
|
# ? Jan 23, 2013 16:55 |
|
DaTroof posted:Close. If they have the same name, the value in $_GET/$_POST will simply be the last one received; but if you bracket the end of it (e.g., <input type="text" name="website[]" />), all the values will be appended to an array. You can also do fun things like this: code:
IT Guy posted:Everything that he said ^ We have an instance which I thought was part of the 0.1%; we store a CSV-list of numbers for a set that we use when adding a new object to the database. e.g. "object_defn [ id=1, something="such and such", subobject_ids="1,2,3,4,17,18,19,20" ]". When the code creates a new object, it pulls out the list of subobjects to make (and each one of those is a new row, so the CSV is only for the definition). After some thought, though, even that could probably be handled better, since a lot of object definitions have the same subobject_ids and we could skip the explode-insert loop in code using INSERT INTO ... SELECT. (The original designers used a lot of this CSV-in-a-database stuff for things that really should have been normalized, and we've fixed all but this one.)
|
# ? Jan 25, 2013 18:38 |
|
I have a new unrelated array_search() problem.PHP code:
PHP code:
quote:Email 0 Twitter 1 Facebook 2 LinkedIn 3 Digg 4 Reddit 5 I think that array_search() is the wrong solution, but I'm not sure where to go from here.
|
# ? Jan 26, 2013 04:09 |
|
I'm tasked with trying to evaluate frameworks to build APIs. Haven't really dealt with them before so is performance a concern with any of the major ones like Yii, Laravel, Symfony, etc? What makes a framework fast or slow? Is performance even a legitimate concern or am I correct in thinking that has less to do with framework and more do to with web server configuration/query optimization?
|
# ? Jan 26, 2013 04:25 |
|
Adraeus posted:I have a new unrelated array_search() problem. Just cut out all of the directory and file operations? <a class="icon" href="'.$url.'" title="'.$label.'"><img class="icon" src="' . $service . '_16.png" /></a> Mister Chief fucked around with this message at 05:20 on Jan 26, 2013 |
# ? Jan 26, 2013 04:49 |
|
Mister Chief posted:Just cut of all of the directory and file operations? I guess that is simpler. I was trying to be fancy. Thanks!
|
# ? Jan 26, 2013 05:12 |
|
nachos posted:I'm tasked with trying to evaluate frameworks to build APIs. Haven't really dealt with them before so is performance a concern with any of the major ones like Yii, Laravel, Symfony, etc? What makes a framework fast or slow? Is performance even a legitimate concern or am I correct in thinking that has less to do with framework and more do to with web server configuration/query optimization? For the most part, it has to do with your hardware, I/O, SSL handshaking, HTTP pipelining and the like. Yes, some frameworks are faster than others, but you'll have other areas to look into before you have to worry about the framework. Just use APC to cache your objects, and memcache or Redis for a memory store and then you can concentrate on speeding up your actual code. I assume you're building REST APIs with these frameworks. If so, I've got a really handy bundle that makes it pretty easy: https://github.com/brightmarch/BrightmarchRestfulBundle
|
# ? Jan 26, 2013 12:20 |
|
php:<?php if (array_key_exists('test',$_GET)) $_GET['test'](); function a() { echo "hello"; } ?> Basically my question is, is this exploitable? I assumed that the answer was yes but it seems like you can't get anything to exploit this. This may be because I'm running the Suhosin patch but I can't tell if that makes a difference. The only negative I see is that you can run phpinfo and see what the server is running.
|
# ? Feb 2, 2013 09:11 |
|
I'm trying to help some WordPress plugin developers troubleshoot a faulty plugin. Here's the relevant section of the code:PHP code:
quote:Fatal error: Call-time pass-by-reference has been removed in /var/www/html/wp-content/plugins/recent-custom-post-type/recent-custom-post-type.php on line 392 Line 392 refers to: PHP code:
I'm not at all familiar with reference variables, so I can't tell what's the best practice here. Any ideas?
|
# ? Feb 2, 2013 10:01 |
|
Adraeus posted:I'm trying to help some WordPress plugin developers troubleshoot a faulty plugin. Here's the relevant section of the code: PHP posted:Note: There is no reference sign on a function call - only on function definitions. Function definitions alone are enough to correctly pass the argument by reference. As of PHP 5.3.0, you will get a warning saying that "call-time pass-by-reference" is deprecated when you use & in foo(&$a);. And as of PHP 5.4.0, call-time pass-by-reference was removed, so using it will raise a fatal error.
|
# ? Feb 2, 2013 10:09 |
|
The Gripper posted:http://php.net/manual/en/language.references.pass.php Wow. Thanks! That was perfect. Maybe now they'll stop insisting that something's wrong with my server.
|
# ? Feb 2, 2013 11:39 |
|
Strong Sauce posted:
Same reason why reqister_globals is really dumb.
|
# ? Feb 2, 2013 22:55 |
|
Hello thread, I am a new student of php with a probably easy/dumb question. I was doing a feature for a little text manipulator that would preg_replace a word with a randomly chosen string from an array. I thought to use rand_array for this, but it seemed that it would pick a random string once, and use that same string in all instances. It seemed to work if I did code:
code:
|
# ? Feb 5, 2013 05:31 |
|
Turkeybone posted:Hello thread, I am a new student of php with a probably easy/dumb question. I was doing a feature for a little text manipulator that would preg_replace a word with a randomly chosen string from an array. I thought to use rand_array for this, but it seemed that it would pick a random string once, and use that same string in all instances. It seemed to work if I did I think that would work, but I believe your first code didn't work because you left out count: PHP code:
|
# ? Feb 5, 2013 06:47 |
|
Oh yes, sorry. I did include that in the code, just an oversight in my copying. Thanks!
|
# ? Feb 5, 2013 06:48 |
|
Turkeybone posted:Hello thread, I am a new student of php with a probably easy/dumb question. I was doing a feature for a little text manipulator that would preg_replace a word with a randomly chosen string from an array. I thought to use rand_array for this, but it seemed that it would pick a random string once, and use that same string in all instances. It seemed to work if I did If you're not using regular expressions to match the string, don't use preg_replace, instead use str_replace. Also, shuffle doesn't return an array - it returns a bool. It actually modifies the original array: php:<? $a = array(1,2,3,4); echo implode(', ', $a)."\n"; // 1, 2, 3, 4 echo $a[0]."\n"; // 1 shuffle($a); echo implode(', ', $a)."\n"; // 3, 2, 1, 4 echo $a[0]."\n"; // 3 ?> php:<? $a = array(1,2,3,4); echo $a[rand(0, count($a)-1)]."\n"; // 3 echo $a[rand(0, count($a)-1)]."\n"; // 2 echo $a[rand(0, count($a)-1)]."\n"; // 3 echo $a[rand(0, count($a)-1)]."\n"; // 1 echo $a[rand(0, count($a)-1)]."\n"; // 4 ?> php:<? $a = array(1,2,3,4); $lastElem = count($a)-1; echo $a[rand(0, lastElem)]."\n"; // 1 echo $a[rand(0, lastElem)]."\n"; // 3 echo $a[rand(0, lastElem)]."\n"; // 3 echo $a[rand(0, lastElem)]."\n"; // 1 echo $a[rand(0, lastElem)]."\n"; // 4 ?>
|
# ? Feb 5, 2013 07:00 |
|
Great, thanks! I did use a very basic regex, but I didn't know about str_replace so that's good, and the rest also makes sense. Thanks again.
|
# ? Feb 5, 2013 07:22 |
|
Turkeybone posted:Great, thanks! I did use a very basic regex, but I didn't know about str_replace so that's good, and the rest also makes sense. Thanks again. Your best friend: http://www.php.net/manual/en/index.php If you search for "replace," you'll get results showing you every way to replace something. e: Also, arrays are the coolest thing ever. You can do so much with arrays. And you can learn quite a bit about PHP by doing what you'd normally do with a few DB calls with only array functions. This is an example of what not to do, but it was great fun, too. Adraeus fucked around with this message at 08:06 on Feb 5, 2013 |
# ? Feb 5, 2013 08:01 |
I've been reading up on MVC but what I've found really helpful are the youtube videos by JREAM to understand how an implementation works in practice. I'm still getting through his videos but wanted to make sure I understand the core concept before I get bogged down in details. Is it perfectly acceptable to have two sub-controllers in an MVC rollout, one for static, one for dynamic? For example: Core website loads via index.php, which calls the bootstrap file, which parses the URL and determines which controller to load, and the model and view (which depends upon default controller behaviour as well as the initial URL call, e.g. domain.com/controller/action/query, action + query being method($args), which would then call the view. That then remains static until a new MVC-link requires a new controller, which loads the entire page again based on the link. Any smaller changes would be done though jquery, referring to divs created during the view. So a jquery command would connect to a separate page with the business logic, process it, and output a view which would replace (or append) upon the target <div> -- without needing to go through the MVC again and reload everything. Does this make sense? I'm designing a website that will have a whole bunch of content that will be manipulated by the user (and saved to/retrieved from an sql database) and need to be updated real-time in small and distinct portions of the screen, without affecting the other data by a reload etc. Is this how MVC websites generally handle both static and dynamic features?
|
|
# ? Feb 5, 2013 12:37 |
|
Adraeus posted:Your best friend: Thanks. Yeah this is where the class is going, our next project is to make a catalog, I assume primarily with arrays. The DB stuff will come in the 2nd half.
|
# ? Feb 5, 2013 14:06 |
|
Turkeybone posted:Thanks. Yeah this is where the class is going, our next project is to make a catalog, I assume primarily with arrays. The DB stuff will come in the 2nd half. If they don't teach you with PDO, scream "YOU'RE DOING IT WRONG!" and throw a table. Seriously, though, no modern PHP course should be teaching database stuff with anything other than PDO.
|
# ? Feb 5, 2013 21:19 |
|
bobthecheese posted:If they don't teach you with PDO, scream "YOU'RE DOING IT WRONG!" and throw a table. Well, it's "Intermediate Web Design", so hopefully it's relevant to reality. I'll report in.
|
# ? Feb 5, 2013 21:24 |
|
Turkeybone posted:Well, it's "Intermediate Web Design", so hopefully it's relevant to reality. I'll report in. Don't worry, it won't (Graduated from a 4 year IT degree and learned more from outside experience )
|
# ? Feb 5, 2013 21:53 |
|
I've just made the switch from CodeIgniter to Laravel 4 beta and boy does the documentation seem to contradict itself a lot. I'm really confused as to whether I should use the routes to handle most the logic or the actual controller themselves. I'm currently playing with Form Validation and the example adds logic to the routes.php file, rather than the actual Controller. php:<? Route::get('register', function() { return View::make('user.register'); }); Route::post('register', function() { $rules = array(...); $validator = Validator::make(Input::all(), $rules); if ($validator->fails()) { return Redirect::to('register')->withErrors($validator); } }); ?> Coming from CI, I would expect the routes.php to simply map to a controller function and build all the logic into there.
|
# ? Feb 5, 2013 23:19 |
|
I haven't used Laravel as extensively as others have, but generally if the logic is small enough that you can do all of it in routes.php then it is OK. It seems this is common when your Laravel code is minimal and most of the work is done in the frontend and Laravel is just there to provide an interface to a database or something. If you have a lot of logic or it has lots of separate parts, then split it up into controllers. Is the beta out yet? Laravel 3 is pretty sweet and 4 seems like a big chance with the reliance on composer and being more broken up.
|
# ? Feb 6, 2013 00:15 |
|
Best practice is to map it to a controller. Writing your logic inside the route itself is very handy for prototyping something or developing a simple tiny website, but know that you're essentially using Laravel as a microframework (think Silex) at that point. It's a really nice feature but definitely not what an MVC framework like Laravel is meant for.
|
# ? Feb 6, 2013 03:59 |
|
Thanks guys. I'll go down that route then. EDIT: You can download the latest beta here and documentation is here. Apparently full release is imminent. SimonNotGarfunkel fucked around with this message at 18:49 on Feb 6, 2013 |
# ? Feb 6, 2013 18:46 |
|
I am having what feels like a pretty basic trying to use PDO for some SQL that I have totally hit a brick wall on From what I have read PDO is adding quotes to the LIMIT command eg '3','10' which is making it not work correctly as it needs to be an integer. From looking around what feels like 100s of sites, I have tried various binds and prepared statements as a solution but I cannot get this LIMIT to work correctly. Everything else works perfectly. Surely there must be a really simple solution for this that I am missing? The basic code is: php:<? $sql= "SELECT * FROM [db].[dbo].[Tb] ORDER by ID LIMIT 3,10 DESC"; foreach ($db->query($sql) as $id) { $page[$count]=$id[1]; $count=$count+1; } ?>
|
# ? Feb 7, 2013 10:47 |
|
Long John Power posted:
Which database engine are you using? The [db].[dbo].[Tb] looks like a MSSQL-ism, which doesn't (so far as I'm aware) support LIMIT 3,10 syntax. In short, though, you answer should be this: php:<? $sql= "SELECT * FROM [db].[dbo].[Tb] ORDER by ID LIMIT 3,10 DESC"; $result = $db->query($sql); foreach ($result->fetchAll() as $id) { $page[$count]=$id[1]; $count ++; } ?> php:<? $sql= "SELECT * FROM [db].[dbo].[Tb] ORDER by ID LIMIT 3,10 DESC"; $result = $db->query($sql); while ($id = $result->fetch()) { $page[$count]=$id[1]; $count ++; } ?> To get the actual data from that, you need to either fetch each row, or fetchAll to get all the rows. As a note, it's not good practice to do a SELECT *, and then access specific, un-named columns. That sets you up for future problems if someone decides to change the schema. Even if this is a once-off project that won't get used again, don't do it because it's a bad habit. bobthecheese fucked around with this message at 11:45 on Feb 7, 2013 |
# ? Feb 7, 2013 11:38 |
|
bobthecheese posted:Which database engine are you using? The [db].[dbo].[Tb] looks like a MSSQL-ism, which doesn't (so far as I'm aware) support LIMIT 3,10 syntax. Thanks for that! We are using PDO but it is through a Quercus install which is basically PHP on Java (don't ask), that is why (I think) it has to be in that format. I scaled down the code from what I am actually using, just to keep it simple. I am actually using named cols on the code. The code still has the problem with the LIMIT being converted to a string by what I am assuming is PDO? http://stackoverflow.com/questions/7772648/php-pdo-limit-clause-issue http://stackoverflow.com/questions/2269840/php-pdo-bindvalue-in-limit/2269931#2269931 They mention various solutions using a bind like this... php:<? $statement->bindValue(':start', $start, PDO::PARAM_INT); $statement->bindValue(':limit', $per, PDO::PARAM_INT); ?> Long John Power fucked around with this message at 12:11 on Feb 7, 2013 |
# ? Feb 7, 2013 12:07 |
|
code:
code:
|
# ? Feb 7, 2013 12:13 |
|
-JS- posted:
Unfortunately not (I just triple checked). Simply adding the LIMIT 3,10 to the end of a working SQL statement causes the error: "Fatal Error: 'fetchAll' is an unknown method of false." Eventually I do want to use placeholders, but I wanted to ensure the LIMIT is working correctly. I guess it could be something going on behind the scenes in the way quercus is running that I might have to look at.
|
# ? Feb 7, 2013 12:23 |
|
Long John Power posted:Unfortunately not (I just triple checked). Simply adding the LIMIT 3,10 to the end of a working SQL statement causes the error: Once again, makes me think that MySQL isn't the database that you're talking to. Try this: php:<? $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "SELECT * FROM [db].[dbo].[Tb] ORDER by ID DESC LIMIT 3,10"; try { $result = $db->query($sql); } catch (PDOException $e) { die ('SQL Error: '.$e->getMessage()); } while ($id = $result->fetch()) { $page[$count]=$id[1]; $count ++; } ?>
|
# ? Feb 7, 2013 12:56 |
|
bobthecheese posted:Once again, makes me think that MySQL isn't the database that you're talking to. Thanks again for the help, I guess that is easily possible given the setup here! That comes back with the errors when limit is used: Warning: function 'PDOException' has 2 required arguments, but only 1 were provided SQL Error: SQLSTATE[SQLSTATE[42000]: Incorrect syntax near 'LIMIT'.]: null and no errors when it isn't used. EDIT: Just found out that this is a MSSQL server that I am connecting to, so that will explain why LIMIT isn't working, lets file this whole thing under schoolboy errors. Long John Power fucked around with this message at 13:21 on Feb 7, 2013 |
# ? Feb 7, 2013 13:17 |
|
Long John Power posted:Thanks again for the help, I guess that is easily possible given the setup here! Learning is learning. You will learn to hate MSSQL :P Get into the practice of using PDO with exceptions, and handling exceptions intelligently (just dying and spitting out the error message isn't really intelligent handling of the error - ideally you should be logging it, and then stopping execution of the code gracefully, but hey - we don't always live in that ideal fantasy world)
|
# ? Feb 7, 2013 13:51 |
|
I am a bad person, tryiing to make PHP do things is not designed to do. I have this happy idea to implemente "prototype oop" to php. Something like this: php:<? $Hail = function(){ function monday(){ return "monday"; } $hello_world = function(){ echo "hello world"; }; $hello_monday = function(){ echo "what a beatiful ".monday()."!"; }; return compact("hello_world","hello_monday"); }; $hoi = $Hail(); $hoi["hello_monday"](); ?> I want my PHP to look more like Javascript, and less like Java or C++ Tei fucked around with this message at 16:57 on Feb 7, 2013 |
# ? Feb 7, 2013 16:54 |
|
Tei posted:I am a bad person, tryiing to make PHP do things is not designed to do. Just learn how to use PHP objects properly, or change to NodeJS. If this is anything more than a 'just for fun'/hobby project, then you're a horrible person, and I'll point you out to YOSPOS. PHP gets a bad enough rap without being JavaScript too By the way, "Prototype OOP" is literally poop.
|
# ? Feb 7, 2013 17:33 |
|
php:<? class Hail { public function __call($method,$args) { $f = $this->$method; return $f(); } public function __construct() { $this->monday = function(){ return "monday"; }; $this->hello_monday = function(){ echo "what a beautiful ".$this->monday()."!"; }; } } $hoi = new Hail(); $hoi->{"hello_monday"}(); ?> The __call magic method is needed because for some reason with these anonymous functions $object->anonymousFunction(); Fails but $function = $object->anonymousFunction; $function(); works "ok". CabaretVoltaire fucked around with this message at 18:20 on Feb 7, 2013 |
# ? Feb 7, 2013 18:17 |
|
|
# ? Jun 9, 2024 02:15 |
|
This is even better / stupider. Have a class like this: php:<? class C { function __call($m, $a = array()) { if (is_callable($this->$m)) { $a[] = $this; return call_user_func_array($this->$m, $a); } else { return $this->m; } } function __construct($f) { foreach ($f as $k => $v) { $this->$k = $v; } } } ?> It just requires the last param of each method to be a pretend "$this" php:<? $o = new C( array( 'monday' => function($th1s){ return "monday"; }, 'hello_monday' => function($th1s){ echo 'hello '.$th1s->monday().'!'; }, 'hello' => function($day, $th1s){ echo 'hello '.$day.'!'; } ) ); $o->{'hello_monday'}(); $o->{'hello'}('tuesday'); ?>
|
# ? Feb 7, 2013 21:29 |