Jump to content

Functions/methods to access the DB ?


jbroussia
 Share

Recommended Posts

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

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.

  • Like 1
Link to comment
Share on other sites

  • 3 years later...

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...