Author Topic: Using SQL in 2.8.3  (Read 1037 times)

Offline richwingerter

  • Posts: 31
Using SQL in 2.8.3
« on: September 29, 2016, 07:59:56 PM »
I have an older version of Website Baker running and I am porting to the latest version, but I'm having trouble getting access to the MySQL DB. I was able to get it before by putting some code into each page like this:

Code:
Only registered users can see contents. Please click here to Register or Login.
I'd like to get access to the SQL functions in the latest version. Has anyone tried this and can help me?

Thanks!

Offline Ruud

  • WebsiteBaker Org e.V.
  • **
  • Posts: 3571
  • Gender: Male
  • Do not use PM for help! Please use the forum!
    • Dev4Me - Professional WebsiteBaker Development
Re: Using SQL in 2.8.3
« Reply #1 on: September 29, 2016, 11:19:42 PM »
Some strange things happening here...

The first require cannot work.
WB_PATH is only set in the config.php that is required_once. So if WB_PATH is defined, the config is already loaded making that line useless.
If not, it will effectively load [SERVER_ROOT]/config.php which is very unlikely to succeed.

The second require is not part of WB, so no telling what will happen there.

The third require is useless too. The same thing as the first one, if WB_PATH is defined, config.php is loaded and the WB initialize script is called too. With that the database classes are already loaded.

The command "load_website_config()" is not a WB function. No telling what should happen there.



So for an external script, just load config.php (make shure your script knows where it is located).
After that, the $database functions are available to that script.

Offline richwingerter

  • Posts: 31
Re: Using SQL in 2.8.3
« Reply #2 on: September 30, 2016, 12:57:22 AM »
This was internal to a page in Website Baker. That is, I went into the Administrator, went to Pages, edited a (code) page, and put that code into the page. So, that would mean that WB_PATH should be set. I think that in the version I was working with I was not able to get any database calls to work without adding these lines within the page.

The more basic question is that I want to execute a SQL call inside the page. So, on the page I would have something like:

Code:
Only registered users can see contents. Please click here to Register or Login.
etc.

Should this work just by adding that code within a page?

Thanks!




Offline Ruud

  • WebsiteBaker Org e.V.
  • **
  • Posts: 3571
  • Gender: Male
  • Do not use PM for help! Please use the forum!
    • Dev4Me - Professional WebsiteBaker Development
Re: Using SQL in 2.8.3
« Reply #3 on: September 30, 2016, 10:48:31 AM »
Should this work just by adding that code within a page?

No. You should use the existing $database functions. (note that the code is executed in a local function scope)
I would suggest to look through other modules code first to learn a bit more about the WB stuctures.

If that is not clear to you, I would strongly advise not to try this kind of php coding yourself.

Offline richwingerter

  • Posts: 31
Re: Using SQL in 2.8.3
« Reply #4 on: September 30, 2016, 10:56:20 PM »
That's what I expected, that it wouldn't work because it's in local scope and I'd have to bring in the definitions to get it working. This worked find for the previous version of Website Baker, because I was able to require the database classes. But I'm not sure where to find those pieces for the current version.

In the current distribution there's a mailing_list.phps file. It uses this call:

Code:
Only registered users can see contents. Please click here to Register or Login.
This is out of context, so I don't know if it's also running in local scope. It does not appear to have any dependencies for mysqli functions.

I tested:

Code:
Only registered users can see contents. Please click here to Register or Login.
(Where my parameters were 'localhost' and the name/password I use to log in to the database through phpMyAdmin.)

All I got was:

Code:
Only registered users can see contents. Please click here to Register or Login.
It's not getting the library.

Do you have a reference to documentation or a specific module that works?



Offline Ruud

  • WebsiteBaker Org e.V.
  • **
  • Posts: 3571
  • Gender: Male
  • Do not use PM for help! Please use the forum!
    • Dev4Me - Professional WebsiteBaker Development
Re: Using SQL in 2.8.3
« Reply #5 on: September 30, 2016, 11:09:33 PM »
I actually was wrong about the local scope. The database class is available for use.

This small test should run fine:
Code:
Only registered users can see contents. Please click here to Register or Login.

Offline richwingerter

  • Posts: 31
Re: Using SQL in 2.8.3
« Reply #6 on: October 08, 2016, 02:58:06 AM »
Excellent! That was just what I needed. Once I had the right code to access a table, I was able to access my tables and get data from them. Thanks a bunch!