Jump to content

$database->getTables() Not Working as Expected


Peter Oehman
 Share

Recommended Posts

When I use $database->getTables() it returns all the fields in our ProcessWire, as long as some other things like modules and templates. I'm relatively new to ProcessWire so I'm not sure if this is what the function is meant to return, however I want the actual tables in our database. Specifically I am looking for a way to access the name of a database without hard coding the name in our sql queries. Any tips are appreciated :)

Link to comment
Share on other sites

Hi Peter, and welcome to the forums!

If you want to get the database name, you can use:

$databaseName = $config->dbName;

Or you could look for $config->dbName in the site's config.php file.

To see the database structure, you could use a tool such as phpMyAdmin or Adminer, or install the Tracy Debugger for ProcessWire module (https://adrianbj.github.io/TracyDebugger/#/) which is highly recommended and, among many other things, adds an Adminer page to the Setup menu.

However, I note that in a typical ProcessWire installation, you don't need to use SQL queries at all, and they're probably not the best way of going about things. Generally, using PW selectors will do everything you need, and will be much more straightforward: https://processwire.com/docs/selectors/. They're one of the really nice features of PW!

Let us know if you need more pointers or suggestions.

  • Like 2
Link to comment
Share on other sites

@Peter Oehman,

Welcome to the forums and to ProcessWire ?.

On 4/6/2022 at 3:40 PM, Peter Oehman said:

$database->getTables()

This will return an array of the names of all tables in your database.

On 4/6/2022 at 3:40 PM, Peter Oehman said:

Specifically I am looking for a way to access the name of a database

This is confusing. Do you want the name of the database or the name of some table in the database?

 

On 4/6/2022 at 3:40 PM, Peter Oehman said:

our sql queries.

In addition to what @BillHstated above and given that you are new to ProcessWire, the first question I'd ask is why? Why do you need to write custom SQL queries? Please let us know. There may  be other ways to achieve what you are after. However, if you do need to use SQL queries, that's fine. ProcessWire also supports that. In which case, prepared statements using the ProcessWire $database API are the way to go.

Link to comment
Share on other sites

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...