|
Massive thanks to those of you who recommend Laravel earlier in this thread. I'm using the L4 beta and it's s real breath of fresh air, especially the Eloquent stuff. I love the way it handles pivot tables and the polymorphic relations stuff. The documentation could be vastly improved in my opinion, but the community seems more than willing to help.
|
# ? Feb 26, 2013 20:33 |
|
|
# ? May 29, 2024 00:37 |
McGlockenshire posted:SQL assembly like that is perfectly fine as long as you have validated that the table name is indeed a valid table name. You can do this either by hard-coding the table list in your code, or looking at information_schema.tables or whatever your DB uses if it doesn't use information_schema. The table names will definitely be hard-coded. I'll most likely be doing a switch($input), where the tables get filled (from a hardcoded list) based on a preg_match of the input word. It's definitely PDO, sorry -- I was super tired and had to completely re-write the entire post because the first one was dumb. When you say placeholder's can't be reused, does that mean I can only use :term once in a query? That sucks a little. If I have to insert that as a PHP variable, I'll have to fully sanitize it before hand. I wouldn't have expected that to be the case.
|
|
# ? Feb 26, 2013 23:43 |
|
Sulla-Marius 88 posted:When you say placeholder's can't be reused, does that mean I can only use :term once in a query? That sucks a little. If I have to insert that as a PHP variable, I'll have to fully sanitize it before hand. I wouldn't have expected that to be the case. Yeah, it's kind of stupid. But no, don't just insert it as a variable, use positional placeholders instead. php:<? $query = 'SELECT foo FROM bar WHERE baz = ? UNION ALL SELECT foo FROM butts WHERE baz = ?'; $sth = $dbh->prepare($query); $sth->execute(array( $baz, $baz )); ?>
|
# ? Feb 27, 2013 05:12 |
|
This is so loving frustrating. I just want to get a bunch of urls generated from an array that's updated often to be processed by PHP. That's it. gently caress. Right now the URLs are in JSON format like the following: code:
code:
- I stringified the array. - Firebug / NET panel tells me the POST is received, I get the success function to fire, the POST body contains all of my data, Firebug even says the data is JSON! [The POST is only received when I have <script type="text/javascript" src="application.js"></script> (i.e., the javascript file containing the AJAX POST above) in the PHP file - is this working as intended?] YET, when I ask PHP to do the most basic of tasks: code:
So I post on StackOverflow. Oh, just add $json_decode! Oh wait, you don't actually need to stringify the JSON before you AJAX it! (>setting data to just divisions breaks the POST). Try var_dump, nothing, try echo $_POST[1]['id'], nothing. I have spent so many hours researching what I'm being told on SO, related questions to my questions on SO, searching Google, but the combination of not formally learning PHP + not having anybody to really ask questions like this is completely killing me at the moment. I keep getting basic answers, but NO results.
|
# ? Feb 27, 2013 06:34 |
hypernova posted:So I post on StackOverflow. Oh, just add $json_decode! Oh wait, you don't actually need to stringify the JSON before you AJAX it! (>setting data to just divisions breaks the POST). What do you mean, breaks the POST? What is the error message? hypernova posted:[The POST is only received when I have <script type="text/javascript" src="application.js"></script> (i.e., the javascript file containing the AJAX POST above) in the PHP file - is this working as intended?] Not sure what you mean here, can you elaborate? Have you tried looking at the raw post data?
|
|
# ? Feb 27, 2013 06:59 |
|
fletcher posted:What do you mean, breaks the POST? What is the error message? 1) If the only thing I change in the ajax is divisionsJSON -> divisions (which is the name of the array that is stringified to divisionsJSON), my POST body in Firebug changes all to undefined=undefined & undefined=undefined etc etc. I tried doing $data = file_get_contents('php://input'); print_r($data); and that returned a blank page. Honestly it feels like something hidden is going on here. I use ASmallOrange shared hosting, would there be any kind of server settings that I would need to change before I can accept POSTs via PHP or something?
|
# ? Feb 27, 2013 07:20 |
|
hypernova posted:1) If the only thing I change in the ajax is divisionsJSON -> divisions (which is the name of the array that is stringified to divisionsJSON), my POST body in Firebug changes all to undefined=undefined & undefined=undefined etc etc. Pretty sure it's you sending the contentType that's loving with it. Drop it, or maybe try specifying the charset.
|
# ? Feb 27, 2013 07:47 |
|
Cartesian_Duelist posted:Pretty sure it's you sending the contentType that's loving with it. Drop it, or maybe try specifying the charset. With data set back to divisions and the contentType dropped, I still get a POST full of undefined.
|
# ? Feb 27, 2013 07:59 |
hypernova posted:With data set back to divisions and the contentType dropped, I still get a POST full of undefined. Try using something else to generate the POST to your test3.php script, like curl or resty. Also, are there any rewrite rules in an .htaccess file at play here? I still don't understand what you mean by this: quote:[The POST is only received when I have <script type="text/javascript" src="application.js"></script> (i.e., the javascript file containing the AJAX POST above) in the PHP file - is this working as intended?] fletcher fucked around with this message at 08:30 on Feb 27, 2013 |
|
# ? Feb 27, 2013 08:28 |
McGlockenshire posted:Yeah, it's kind of stupid. But no, don't just insert it as a variable, use positional placeholders instead. Ah jeez, I see. So I gotta pass it some dumb lookin' arrays so that the unions can function: array("table1", "buttz", "table2", "buttz", "table3", "buttz", "table4", "buttz"); And just insert into the array as many pairs of "tablex" and "buttz" as there are SELECT statements. So it'll wind up looking like: php:<? $query = 'SELECT foo FROM ? WHERE baz = ? UNION ALL SELECT foo FROM ? WHERE baz = ?'; $sth = $dbh->prepare($query); $sth->execute(array( $table1, $baz, $table2, $baz )); ?> Thanks for your help.
|
|
# ? Feb 27, 2013 09:47 |
|
Sulla-Marius 88 posted:Ah jeez, I see. So I gotta pass it some dumb lookin' arrays so that the unions can function: Nope, because you can't insert tables like that! So you still need to whitelist tablenames and concat the sql.
|
# ? Feb 27, 2013 10:54 |
|
Sulla-Marius 88 posted:So it'll wind up looking like: Just to clarify what KARMA! said, you can't bind identifiers (table & column names). Only values can be bound. You'd need to SELECT foo FROM $table1 WHERE baz = ? UNION ALL SELECT foo FROM $table2 WHERE baz = ?
|
# ? Feb 27, 2013 11:13 |
KARMA! posted:Nope, because you can't insert tables like that! So you still need to whitelist tablenames and concat the sql. php:<? $tables = array("table1", "table2", "table3"); $column = "DBColumnName"; $count = count($tables); $statement = "SELECT *, {$tables[0]} as table FROM {$tables[0]} WHERE {$column} LIKE ?"; // ensure that it always passes at least one query table $i = 1; // skip the first table that we already accounted for while ($i < $count) { $statement .= " UNION ALL "; $statement .= "SELECT *, {$tables[$i]} as table FROM {$tables[$i]} WHERE {$column} LIKE ?"; // every extra table gets a UNION ALL then the query repeated } $sth = $this->db->prepare($statement); $exec = array(); $x = 0; while ($x < $count) { $exec[] = "searchterm"; // the search term that gets bound as each instance of ? above $x++; } // $exec now has as many "searchterms" as there are ?'s in the above $sth->execute($exec); ?>
|
|
# ? Feb 27, 2013 11:27 |
|
The random tabbing and extraneous loops aside, yep.php:<? $column = "DBColumnName"; $statements = array(); $bound_input = array(); foreach(array("table1", "table2", "table3") as $table) { $statements[] = "SELECT *, '{$table}' as table FROM {$table} WHERE {$column} LIKE ?"; $bound_input[] = '%a search term%'; } $sql = implode(' UNION ALL ', $statements); $sth = $this->db->prepare($sql); $sth->execute($bound_input); ?>
|
# ? Feb 27, 2013 15:59 |
|
Sulla-Marius 88 posted:
This is a minor nitpick I think, I just find ?'s throughout the queries annoying, but is there any reason no one has recommended using named params, then using an associative array? php:<? $query = 'SELECT foo FROM :table WHERE baz = :baz UNION ALL SELECT foo FROM :table2 WHERE baz = :baz'; $sth = $dbh->prepare($query); $sth->execute(array("table" => $table1,"baz" => $baz,"table2" =>$table2)); ?>
|
# ? Mar 1, 2013 04:09 |
I agree ATM Machine. When you go back to modify the code a year later you are stuck counting question marks and it's a big pain in the rear end. I definitely prefer the named parameters.
|
|
# ? Mar 1, 2013 04:22 |
|
Personally? I prefer to use the absolute long-form for prepared queries, which allows some... interesting codephp:<? $v = 1; $query = $__CONNECTION->prepare("SELECT count(`id`) as count FROM `".DB_PREF."settings` WHERE `setting`=:setting AND `value`=:value GROUP BY `value`"); $query->bindValue(':hash', self::key(), PDO::PARAM_STR); $query->bindParam(':visible', $v, PDO::PARAM_INT); $vis = array('totalvisible'=>1, 'killed'=>0, 'autokilled'=>-1, 'spam'=>-2); $breakdown = array(); foreach ($vis as $k=>$v) { $query->execute(); $breakdown[$k] = 0; if ($r = $query->fetch(PDO::FETCH_ASSOC)) { $breakdown[$k] = $r['count']; } } ?> It's possibly a little confusing for people who are unfamiliar with the difference between "bindValue" and "bindParam", but I like it.
|
# ? Mar 1, 2013 04:31 |
|
I'm freaking out over here. I was just handed a project to help with loading a database using CodeIgniter. The database has only about 2500 records but the previous guy made it so you can only load 20 records at a time. I am using the jQuery DataTables plugin for displaying records but the sort is broken since only 20 records come up per page. Pagination is being handled by CI as well. If I try increasing the amount of records loaded it considerably slows down the page load. Any idea on how to load all records but not have the system crawl? DataMapper, which is being used, shows some alternate methods (right now get() is being used with 20 being thrown in for the $count but as mentioned, increasing that slows things down) like get_iterated() but it doesn't seem to increase performance. Here is the code being used to query things from the Model. PHP code:
|
# ? Mar 1, 2013 17:54 |
|
Vintersorg posted:I'm freaking out over here. The problem is most definitely in the model or some type of configuration. DataMapper is very fast. 2500 records is not very much at all. Can you post your model?
|
# ? Mar 1, 2013 21:25 |
|
I'm really new to PHP and I'm trying to create a simple script that generated a random number, gives the users 5 tries to guess it, and then generate a new number using a session, but I hit a bit of a bump; for whatever reason, in the middle of my first comparison, it just starts outputting the rest of the php to the screen. I make the comparisonPHP code:
E: Wait, nevermind, I'm good. I was previewing an old version of the file while trying to debug a newer version of the file saved elsewhere. All good now, newer version of the file does not have this problem. burnishedfume fucked around with this message at 23:04 on Mar 2, 2013 |
# ? Mar 2, 2013 22:34 |
|
Post the whole thing.
|
# ? Mar 2, 2013 23:00 |
|
Suddenly, my server is returning pages with a blank line at the top of every page (only on PHP scripts). I assumed this had to do with my code, but even if I isolate a page away from the CMS I can see the blank line at the top- so this doesn't seem to do with WordPress or any other specific code if a completely independent script is returning the same error. Test: With just simple HTML (no <?php ?> tags) in both an .html file and another with a .php extension, the source looks normal in html but with the php extension the output starts on line 2. The config file looks like it hasn't changed. Any ideas?
|
# ? Mar 4, 2013 19:29 |
In your isolated script, try adding this to the type (do not do this on production or publicly accessible pages, some error messages will leak your database credentials and things like that):code:
|
|
# ? Mar 4, 2013 21:26 |
|
fletcher posted:In your isolated script, try adding this to the type (do not do this on production or publicly accessible pages, some error messages will leak your database credentials and things like that): Thanks. I'll keep digging in the error logs. The php error code didn't reveal anything.
|
# ? Mar 4, 2013 21:59 |
|
klem_johansen posted:Suddenly, my server is returning pages with a blank line at the top of every page (only on PHP scripts). I assumed this had to do with my code, but even if I isolate a page away from the CMS I can see the blank line at the top- so this doesn't seem to do with WordPress or any other specific code if a completely independent script is returning the same error. What are you encoding in? You're probably getting a byte-order-mark at the top of your page. Set the encoding to UTF-8 without BOM and it should go away.
|
# ? Mar 5, 2013 03:12 |
|
klem_johansen posted:Suddenly, my server is returning pages with a blank line at the top of every page (only on PHP scripts). I assumed this had to do with my code, but even if I isolate a page away from the CMS I can see the blank line at the top- so this doesn't seem to do with WordPress or any other specific code if a completely independent script is returning the same error. If you have a PHP script which looks like this: code:
|
# ? Mar 5, 2013 11:09 |
|
qntm posted:If you have a PHP script which looks like this: From what I understand, his test was of a plain html file, renamed as a PHP file, and the output was altered despite no PHP being used. I think it sounds like something is happening server side to alter the page by trying to insert something after a request for a PHP file is made, whatever its trying to insert has been removed or prevented, but whatever is doing it hasn't, so its adding in a blank newline. Of course, maybe its something simple like a rogue php.ini file that's caused a setting to be overwritten somewhere. If its possible, can you copy everything onto a local server like XAMPP or something similar and see if it produces the same results? ATM Machine fucked around with this message at 14:52 on Mar 5, 2013 |
# ? Mar 5, 2013 14:45 |
|
Cartesian_Duelist posted:The problem is most definitely in the model or some type of configuration. DataMapper is very fast. 2500 records is not very much at all. Can you post your model? There is 5 separate models being referenced. I'll link to them and see if that helps but I am not sure what we can do other then a total code re-write here. The way this thing was designed is that it's referencing 5 different tables. trades id subscription_id trades_details id trade_id trade_name ratings trade_id subscriptions id subscription_type membership_types id membership_type Table being displayed: Here is the main controller (admin_trades.php) http://pastebin.com/JpjxHLaU This is the view (admin_trades_browse.php) http://pastebin.com/QAq7j3j9 These are the various models being called (it's a mess, I hate them and wish I was never left with them) Model->Trade http://pastebin.com/DFDsF1TA Model->Trades_Detail http://pastebin.com/RDe49uvw Model->Ratings http://pastebin.com/jQ3rkLDG Model->Membership_Type http://pastebin.com/k49LdviN Model->Subscription http://pastebin.com/6qtrbCXH I am pretty much posting out of desperation. Unsure why it's doing this. Am I asking too much by wanting to sort the fields by rating?
|
# ? Mar 5, 2013 22:30 |
|
ATM Machine posted:I think it sounds like something is happening server side to alter the page by trying to insert something after a request for a PHP file is made, whatever its trying to insert has been removed or prevented, but whatever is doing it hasn't, so its adding in a blank newline. Wordpress was mentioned; I'm starting to wonder if it's possibly a rogue file somewhere that's doing it. Maybe it's something like auto-prepend-file in a php.ini or .htaccess somewhere. Try creating a page with just <?php phpinfo(); and see if it gives you any information. If that doesn't, try this: php:<? ini_set('display_errors','1'); ini_set('display_startup_errors','1'); error_reporting (E_ALL); header("Content-type: text/plain"); print "Test"; ?> This is all voodoo debugging, though. Good luck finding the cause.
|
# ? Mar 6, 2013 00:29 |
|
Does anyone have a suggestion for a(n API) documenter? Been messing around with phpDocumenter, but it's kind of low-rent. My boss wants something that looks like Yii's: http://www.yiiframework.com/doc/api/ Unfortunately it looks like Yii's is probably an app using their own framework or something. They released a version for others to create static pages, but it seems to only export the actual framework and nothing from our app. As well it hasn't been updated in two years, and isn't nearly as nice as what they have currently published. ApiGen looks like it has a lot of potential followed by phpDocumenter2. I've also looked at Doxygen, HeaderDoc, ROBODoc, ThimbleDoc & TwinText. I think I'm just going to have to beat either ApiGen or phpDocumenter2 into submission. teethgrinder fucked around with this message at 23:10 on Mar 6, 2013 |
# ? Mar 6, 2013 23:04 |
|
teethgrinder posted:Does anyone have a suggestion for a(n API) documenter? Been messing around with phpDocumenter, but it's kind of low-rent. My boss wants something that looks like Yii's: http://www.yiiframework.com/doc/api/ Zym has a nice template for phpDocumentor that's similar to Yii's design: http://zymengine.com/dev/news/30-phpdoc-extjs-converter-template
|
# ? Mar 6, 2013 23:29 |
|
Hi guys, Can I get some help working through a 2D $_POST array and PDO insert queries? Right now, I have a 2D Post array that looks like this: code:
I've tried a foreach and a for, but it's not inserting and I'm getting a reference error in the error log. Where am I going wrong here? Fluue fucked around with this message at 06:42 on Mar 7, 2013 |
# ? Mar 7, 2013 06:39 |
|
You should post your code first so we can tell you what's going wrong.
|
# ? Mar 7, 2013 09:02 |
|
KNITS MY FEEDS posted:You should post your code first so we can tell you what's going wrong. http://pastebin.com/LkQfNBUn The commented out SQL statements are from an earlier version of this when the server didn't have support for PDO. I was thinking that looping for $i would do the trick (by binding, then executing), but the script never inserts data to the database.
|
# ? Mar 7, 2013 17:46 |
|
Zamujasa posted:Wordpress was mentioned; I'm starting to wonder if it's possibly a rogue file somewhere that's doing it. Maybe it's something like auto-prepend-file in a php.ini or .htaccess somewhere. I didn't see an error on either page. On the example above, I just see "test" with the source starting on line 2. As a workaround I used ob_get_clean to get the pdf exporters working again, but I'd really like to know why this happened.
|
# ? Mar 7, 2013 20:59 |
|
Fluue posted:http://pastebin.com/LkQfNBUn I'm still getting an error saying my loop goes out of bounds when referencing the array. The 2D array, at most, has a $_POST['fieldname'][0] and $_POST['fieldname][1], yet the code seems to think I'm going to up to 2. I'm thinking it's maybe how I'm counting the loop, but I'm not sure. Errors aren't being very helpful.
|
# ? Mar 8, 2013 17:38 |
|
Fluue posted:I'm still getting an error saying my loop goes out of bounds when referencing the array. The 2D array, at most, has a $_POST['fieldname'][0] and $_POST['fieldname][1], yet the code seems to think I'm going to up to 2. I'm thinking it's maybe how I'm counting the loop, but I'm not sure. Errors aren't being very helpful. Here's where your code is going wrong: code:
code:
|
# ? Mar 8, 2013 19:04 |
Using PDO, why does "SELECT name FROM" work while "SELECT * FROM" doesn't...? E.g. php:<? $column = "columnname"; $table = array("table1", "table2"); $word = "blah"; $statements = array(); foreach ($tables as $table) { $statements[] = "SELECT * FROM {$table} WHERE {$column} LIKE :searchword"; } $sql = implode(' UNION ALL ', $statements); try { $sth = $this->db->prepare($sql); $sth->execute(array("searchword" => $word)); $data = $sth->fetchAll(); return $data; } catch (Exception $e) { echo "devmode. error: " . $e->getMessage(); } } ?> If I change the asterisk to a column name it works, but using the asterisk a var_dump on $data returns array(0) {}.
|
|
# ? Mar 10, 2013 05:17 |
|
Do the tables have the same columns in the same order? Union wants to combine things which have the same structure, which would explain why combining a single column from each table works, but trying to do it to everything fails.
|
# ? Mar 10, 2013 06:01 |
|
|
# ? May 29, 2024 00:37 |
|
You also forgot to tell us what you mean when you say that it "doesn't work."
|
# ? Mar 10, 2013 07:45 |