1
bb2120
Integrating MySQL using PHP scripts into XOOPS blocks
  • 2005/11/14 17:08

  • bb2120

  • Not too shy to talk

  • Posts: 179

  • Since: 2005/7/6 1


I want to create a relatively simple script containing 3 MySQL queries.

Do I just assume that the connection to the database has already been opened, or do I need to do something like this at the start?

Quote:
include 'http://www.wazaa.co.uk/databasefactory.php'


I think databasefactory.php is XOOPS' main database connection script.
Also, when writing PHP in custom blocks, do I need to start with , or can I get straight into the script?

Could someone please tell me hw to write MySQL scripts in custom blocks, or send me a script that they have already made.

Thanks in advance

2
Dave_L
Re: Integrating MySQL using PHP scripts into XOOPS blocks
  • 2005/11/14 17:46

  • Dave_L

  • XOOPS is my life!

  • Posts: 2277

  • Since: 2003/11/7


Yes, you can assume that the connection is already established.

You don't need to include any files.

Do not use the PHP brackets.

Here's an example:

global $xoopsDB;
$users_table $xoopsDB->prefix('users');
$result $xoopsDB->query("
   SELECT COUNT(*)
   FROM   
$users_table
   WHERE  level > 0
"
);
list(
$usercount) = $xoopsDB->fetchRow($result);
$xoopsDB->freeRecordSet($result);

echo 
"There are $usercount registered users.";


That's actually a bad example, because whenever possible, it's better to use the core classes instead of explicit database queries. Here's how the same thing would be accomplished with the core classes:

$member_handler =& xoops_gethandler('member');
$criteria  =  new Criteria('level'0'>');
$usercount =  $member_handler->getUserCount($criteria);

echo 
"There are $usercount registered users.";

3
bb2120
Re: Integrating MySQL using PHP scripts into XOOPS blocks
  • 2005/11/14 20:21

  • bb2120

  • Not too shy to talk

  • Posts: 179

  • Since: 2005/7/6 1


Many thanks for the straight forward and in depth answer

4
bb2120
Re: Integrating MySQL using PHP scripts into XOOPS blocks
  • 2005/11/14 21:37

  • bb2120

  • Not too shy to talk

  • Posts: 179

  • Since: 2005/7/6 1


I am trying to make a custom block that displays a random game (with links, image, and title) from my mydownloads module. I understand the code to query, but not when smarty variables start coming into it. Should I query, or edit the module files to make a new smarty variable, and then call that in the block?

How should I go about doing this?

5
Dave_L
Re: Integrating MySQL using PHP scripts into XOOPS blocks
  • 2005/11/14 22:43

  • Dave_L

  • XOOPS is my life!

  • Posts: 2277

  • Since: 2003/11/7


Smarty template variables are not involved here.

By "core classes", I was referring to using the various facilities built into XOOPS for retrieving information from the database.

The first example I posted queried the users table explicitly. This is a bad idea because if the users table structure gets changed in a XOOPS upgrade, as it did between XOOPS 2.0.x and XOOPS 2.2.x, then the query might stop working. But by using the member handler class, it's much more likely that the code will remain compatible with future versions of XOOPS.

Failure to make proper use of the core classes is probably the main reason that many modules written for XOOPS 2.0.x are not compatible with XOOPS 2.2.x.

In your case, you may not have a choice. The mydownloads module may not provide any way to access the database tables except by explicit queries. But as long as you're only accessing the tables specific to the mydownloads module, and not the core tables, then you shouldn't run into problems, unless you upgrade to a new version of the mydownloads module that changes its tables' structure.

Login

Who's Online

299 user(s) are online (183 user(s) are browsing Support Forums)


Members: 0


Guests: 299


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Nov 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits