WebsiteBaker Community Forum

WebsiteBaker Support (2.8.x) => Templates, Menus & Design => Topic started by: richwingerter on September 29, 2016, 07:59:56 PM

Title: Using SQL in 2.8.3
Post by: richwingerter 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: [Select]
require_once(WB_PATH.'/config.php');
require_once(WB_PATH.'/manager/main.functions.php');
require_once(WB_PATH.'/framework/class.database.php');
load_website_config();

I'd like to get access to the SQL functions in the latest version. Has anyone tried this and can help me?

Thanks!
Title: Re: Using SQL in 2.8.3
Post by: Ruud 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.
Title: Re: Using SQL in 2.8.3
Post by: richwingerter 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: [Select]
$mysql = new mysql;
$sql = "SELECT ...";
$res =  $mysql->query($sql);

etc.

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

Thanks!



Title: Re: Using SQL in 2.8.3
Post by: Ruud 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.
Title: Re: Using SQL in 2.8.3
Post by: richwingerter 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: [Select]
$mysql = mysqli_connect('localhost', 'username', 'password');
mysqli_select_db($mysql, 'mydb');
etc.

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: [Select]
$db = mysqli_connect( my parameters);
if(!$db) {
   die("Could not connect: " . $mysqli_error());
}

(Where my parameters were 'localhost' and the name/password I use to log in to the database through phpMyAdmin.)

All I got was:

Code: [Select]
Could not connect:
It's not getting the library.

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


Title: Re: Using SQL in 2.8.3
Post by: Ruud 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: [Select]
<?php /* open tag used for code detection in the forum */

$query $database->query("SELECT `page_title` from ".TABLE_PREFIX."pages where `page_id`='1' ") ;
if(
$query && $query->numRows() == 1) {
  
$row $query->fetchRow();
  echo 
$row['page_title'];
} else {
  echo 
"no data";
}
Title: Re: Using SQL in 2.8.3
Post by: richwingerter 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!