jbroussia Posted December 30, 2010 Share Posted December 30, 2010 Hi, Does ProcessWire provide functions/methods to access the DB (connecting, querying) ? Is it possible to use PW to manage (read/write) custom tables ? Also, how did you process all the data you put in the demo ? I mean, you didn't manually create and fill a page for each city/architect/skycraper ? Thanks. Link to comment Share on other sites More sharing options...
ryan Posted December 30, 2010 Share Posted December 30, 2010 Does ProcessWire provide functions/methods to access the DB (connecting, querying) ? Is it possible to use PW to manage (read/write) custom tables ? ProcessWire's Database class is extended from PHP's mysqli, so when you access the database you are dealing with a mysqli connection and all the functions and info in the PHP manual applies. From all templates, the $db instance is scoped locally, so you can do this (example): $result = $db->query("SELECT id, name, data FROM some_table"); while($row = $result->fetch_array()) print_r($row); From modules or any class extended from Wire, the database is scoped via $this->db: $result = $this->db->query("SELECT id, name, data FROM some_table"); while($row = $result->fetch_array()) print_r($row); From outside ProcessWire classes or in any regular function or non-ProcessWire class, the database can be accessed from the wire(...) function: $result = wire('db')->query("SELECT id, name, data FROM some_table"); while($row = $result->fetch_array()) print_r($row); Intended for use outside of ProcessWire templates and classes, that wire() function also provides access to all of ProcessWire's API variables, like $page, $pages, and others... i.e. wire('pages')->find('selector'); Also I should mention that I've never needed to use the $db from my templates. While it's there and ready for you to use, it's always preferable (not to mention easier and safer) to use ProcessWire's API for accessing any of it's data. If you need to create a connection to another database, then make a new mysqli connection (or PDO, regular mysql, etc, according to your preference). ProcessWire does not try to replace PHP's database functions. Also, how did you process all the data you put in the demo ? I mean, you didn't manually create and fill a page for each city/architect/skycraper ? Good question, I've written a reply, but going to paste in a new thread so that the subject line matches the content. 1 Link to comment Share on other sites More sharing options...
joer80 Posted May 27, 2014 Share Posted May 27, 2014 Does using ProcessWire's API to access the db make the query injection safe? Link to comment Share on other sites More sharing options...
joer80 Posted May 27, 2014 Share Posted May 27, 2014 Also, if I go the non api route and pass in a url query string, is there a process wire way to escape it? Link to comment Share on other sites More sharing options...
apeisa Posted May 27, 2014 Share Posted May 27, 2014 Direct database access is just a PDO, nothing special there. 1 Link to comment Share on other sites More sharing options...
joer80 Posted May 29, 2014 Share Posted May 29, 2014 I saw the cheat sheet shows the sanitize methods! http://cheatsheet.processwire.com/ Link to comment Share on other sites More sharing options...
adrian Posted May 29, 2014 Share Posted May 29, 2014 Also, a little more? info here: http://processwire.com/api/variables/sanitizer/ Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now