Using SQL in 2.8.3

richwingerter

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!

Ruud

I actually was wrong about the local scope. The database class is available for use.

This small test should run fine:
<?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";
}
[url=https://dev4me.com/modules-snippets/]Dev4me - WebsiteBaker modules[/url] - [url=https://wbhelp.org/]WBhelp.org[/url]

richwingerter

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:

$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:

$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:

Could not connect:

It's not getting the library.

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



Ruud

Quote from: richwingerter on September 30, 2016, 12:57:22 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.
[url=https://dev4me.com/modules-snippets/]Dev4me - WebsiteBaker modules[/url] - [url=https://wbhelp.org/]WBhelp.org[/url]

richwingerter

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:

$mysql = new mysql;
$sql = "SELECT ...";
$res =  $mysql->query($sql);


etc.

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

Thanks!




Ruud

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.
[url=https://dev4me.com/modules-snippets/]Dev4me - WebsiteBaker modules[/url] - [url=https://wbhelp.org/]WBhelp.org[/url]

richwingerter

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:

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!