Connect to a db via a UDT [SOLVED with work around]

For questions and problems with the CMS core. This board is NOT for any 3rd party modules, addons, PHP scripts or anything NOT distributed with the CMS made simple package itself.
User avatar
Gregor
Power Poster
Power Poster
Posts: 1874
Joined: Thu Mar 23, 2006 9:25 am

Connect to a db via a UDT [SOLVED with work around]

Post by Gregor »

Hello,

Is it possible to connect to a database via a udt? The code in the udt looks like:

Code: Select all

$CLASS["db"]->connect();
The file class_mysql.php has the following function:

Code: Select all

function connect() {
        $this->conn_id = mysql_connect($this->dbms['host'].":".$this->port,$this->dbms['user'],$this->dbms['pass']); 
        if ($this->conn_id == 0) {
            $this->sql_error("Connection Error, kan geen verbinding met DB maken");
        }
        if (!mysql_select_db($this->dbms['dbName'], $this->conn_id)) {
            $this->sql_error("Database Error");
        }
        return $this->conn_id;
    }
The error that occurs is that it is "Database Error". I checked the parameters that are in the UDT, and they are correct. Am I overlooking something, knowing that I'm not a php-programmer ;)

Thanks for your help.

G
Last edited by Gregor on Wed Mar 28, 2007 1:28 pm, edited 1 time in total.
Vin

Re: Connect to a db via a UDT

Post by Vin »

Huh? UDTs are not php files, they are actually stored in the database...if you store the code in the files, you create plugins... (OK, that was a little quimble ;))

Try $db = &$gCms->db; to connect to the cmsms database. That works  8).
User avatar
Gregor
Power Poster
Power Poster
Posts: 1874
Joined: Thu Mar 23, 2006 9:25 am

Re: Connect to a db via a UDT

Post by Gregor »

Thanks Vin, but oeps, you're losing me....

This is the whole code, 2 parts. The first code I put in a User Defined Tag without the php-tag and put in a template, like {poll_include} The seconde part, I also put in a UDT, and called it {poll}.

Code: Select all

<?php
/* Include this before your html code */
include_once "./poll_cookie.php";
?>

Code: Select all

<?php
/* path */
$poll_path = dirname(__FILE__);

require_once $poll_path."/include/config.inc.php";
require_once $poll_path."/include/$POLLDB[class]";
require_once $poll_path."/include/class_poll.php";
$CLASS["db"] = new polldb_sql;
$CLASS["db"]->connect();

$php_poll = new poll();

/* the first poll */
echo $php_poll->poll_process(1);

?>
If I understand you correct... than it is not possible to have the code in the UDT? Where should I put it??? Second, your
$db = &$gCms->db;
is that a replacement for the function or for the $CLASS?

Thanks for your help,
G
Dee
Power Poster
Power Poster
Posts: 1197
Joined: Sun Mar 19, 2006 8:46 pm

Re: Connect to a db via a UDT

Post by Dee »

Try closing the db (add $CLASS["db"]->close_db(); at the end of the script):

Code: Select all

<?php
/* path */
$poll_path = dirname(__FILE__);

require_once $poll_path."/include/config.inc.php";
require_once $poll_path."/include/$POLLDB[class]";
require_once $poll_path."/include/class_poll.php";
$CLASS["db"] = new polldb_sql;
$CLASS["db"]->connect();

$php_poll = new poll();

/* the first poll */
echo $php_poll->poll_process(1);

$CLASS["db"]->close_db();
?>
If that doesn't work, re-open the CMSMS database connection (add adodb_connect(); at the end of the script):

Code: Select all

<?php
/* path */
$poll_path = dirname(__FILE__);

require_once $poll_path."/include/config.inc.php";
require_once $poll_path."/include/$POLLDB[class]";
require_once $poll_path."/include/class_poll.php";
$CLASS["db"] = new polldb_sql;
$CLASS["db"]->connect();

$php_poll = new poll();

/* the first poll */
echo $php_poll->poll_process(1);

adodb_connect();
?>
Regards,
D
User avatar
Gregor
Power Poster
Power Poster
Posts: 1874
Joined: Thu Mar 23, 2006 9:25 am

Re: Connect to a db via a UDT

Post by Gregor »

Thanks for you suggestion, Dee.

Both your suggestions leeds to this error:
Fatal error: Call to a member function on a non-object in /home/httpd/vhosts/uisge-beatha.eu/httpdocs/lib/content.functions.php(669) : eval()'d code on line 16

The code in {include_poll} is:

Code: Select all

/* Include this before your html code */
include_once "/home/httpd/vhosts/uisge-beatha.eu/httpdocs/poll/poll_cookie.php";
I placed this tag here:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<__html xmlns="http://www.w3.org/1999/xhtml" xml:lang="nl" lang="nl">
<!-- Change lang="en" to the language of your site -->

<head>

<!-- UDT voor het poll-systeem 21mrt07 -->
{include_poll}

<title>{sitename} - {title}</title>
<!-- The sitename is changed in Site Admin/Global settings. {title} is the name of each page -->

The code in {poll} is (after Dee's suggestion):

Code: Select all

/* path */ 
$poll_path = "/home/httpd/vhosts/uisge-beatha.eu/httpdocs/poll";

require_once $poll_path."/include/config.inc.php";
require_once $poll_path."/include/$POLLDB[class]";
require_once $poll_path."/include/class_poll.php";

$CLASS["db"] = new polldb_sql;
$db =& $gCms->polldb_sql;

/* $CLASS["db"]->connect();  */

$db =& $gCms->db;

/* the first poll */ 
echo $php_poll->poll_process(1);

adodb_connect();
I placed this tag in the navigation section of the template, because for now I'd like to have under the menu.

Based on the error, I think it error's at this line:

Code: Select all

echo $php_poll->poll_process(1);
BTW, to see it working outside CMSMS
http://www.uisge-beatha.eu/poll/demo_1.php

I created a test template that is not accessable through the menu: http://www.uisge-beatha.eu/index.php?page=test

Any suggestions for improvement?
Thanks so far,
Gregor
Last edited by Gregor on Fri Mar 23, 2007 5:50 am, edited 1 time in total.
User avatar
Gregor
Power Poster
Power Poster
Posts: 1874
Joined: Thu Mar 23, 2006 9:25 am

Re: Connect to a db via a UDT

Post by Gregor »

Hello,

Without trying to be to impatient ;)  does someone have a suggestion how to take it from here?

Thanks for your help,
G
Vin

Re: Connect to a db via a UDT

Post by Vin »

Well, you can use either the way of the $gCms variable (which is global, so you have to call it as global $gCms; first) and use its functions, or to connect to the database with your own.
That $db = &$gCms->db; is due to the access to the adodb functions AND cmsms stuff - http://wiki.cmsmadesimple.org/index.php/User_Handbook/Admin_Panel/Extensions/User_Defined_Tags.
So that error is IMHO caused by omitting the global $gCms; but it's not neccessary to use this. Try Dee's solution without using the $gCms.
User avatar
Gregor
Power Poster
Power Poster
Posts: 1874
Joined: Thu Mar 23, 2006 9:25 am

Re: Connect to a db via a UDT

Post by Gregor »

Thanks! I read the Wiki, however I find it difficult to understand since I'm not a programmer. Removing "&$gCms->" leads to the same error as mentioned previous.

I noticed, that the error occurs on this line:

Code: Select all

/* the first poll */ 
echo $php_poll->poll_process(1);
Any suggestions I might try?

Thanks for thinking with me to find a solution.
G
Last edited by Gregor on Sun Mar 25, 2007 4:14 pm, edited 1 time in total.
Vin

Re: Connect to a db via a UDT

Post by Vin »

Ah, I didn't look carefully... you'd already posted where the line is.
Hmm, the error says that there is a member function call on a non-object... are you sure you'd initialized the variable $php_poll?
Gregor wrote:

Code: Select all

<?php
/* Include this before your html code */
include_once "./poll_cookie.php";
?>

Code: Select all

<?php
/* path */
$poll_path = dirname(__FILE__);

require_once $poll_path."/include/config.inc.php";
require_once $poll_path."/include/$POLLDB[class]";
require_once $poll_path."/include/class_poll.php";
$CLASS["db"] = new polldb_sql;
$CLASS["db"]->connect();

$php_poll = new poll();

/* the first poll */
echo $php_poll->poll_process(1);

?>
Aren't you missing the $php_poll = new poll(); you'd had once?
User avatar
Gregor
Power Poster
Power Poster
Posts: 1874
Joined: Thu Mar 23, 2006 9:25 am

Re: Connect to a db via a UDT

Post by Gregor »

oops ::)

We are improving. It now says that it has a database error.
Database Error

MySQL Error : Database Error
Error Number: 1046 No Database Selected
Date        : Sun, March 25, 2007 19:52:49
IP          : 83.84.87.33
Browser    : Mozilla/5.0 (Windows; U; Windows NT 5.1; nl; rv:1.8.1.2) Gecko/20070219 Firefox/2.0.0.2
Referer    :
PHP Version : 4.3.11
OS          : Linux
Server      : Apache
Server Name : www.uisge-beatha.eu
Script Name : /index.php
I copied your code (Vin). It seems that the variables are not passed (are we back where we started ;) ) Reading the Wiki, I don't get the picture of how to pass the variables from the UDT to external php-software.

http://www.uisge-beatha.eu/index.php?page=test

Gregor
Last edited by Gregor on Mon Mar 26, 2007 2:08 pm, edited 1 time in total.
Vin

Re: Connect to a db via a UDT

Post by Vin »

It was your own code, you just omit a variable by accident...
If the UDT is the issue, then you can try making it as a plugin (a flat php file).
You'd have to wrap the whole code in a

Code: Select all

function_smarty_cms_function_<the name of the tag>($params, &$smarty){
<the code>
}
and place it into the /plugins/ folder.
However, me thinks the issue is somewhere else... is the database selected properly? I don't know the code of the poll, so I can't say if you do refer to the right variables and functions...
Last edited by Vin on Mon Mar 26, 2007 8:00 pm, edited 1 time in total.
User avatar
Gregor
Power Poster
Power Poster
Posts: 1874
Joined: Thu Mar 23, 2006 9:25 am

Re: Connect to a db via a UDT

Post by Gregor »

Thanks for your repley Vin!

Just a quick reply. I'll have a look at your suggestion (what an optionns does cmsms have :) ).

It errors on the line:

Code: Select all

if (!mysql_select_db($this->db['dbName'], $this->conn_id)) { 
The file that is called from the udt, is "class_mysql.php"i:

Code: Select all

<?php
/**
 * ----------------------------------------------
 * Advanced Poll 2.0.7 (PHP/MySQL)
 * Copyright (c) Chi Kien Uong
 * URL: http://www.proxy2.de
 * ----------------------------------------------
 
echo $POLLDB["dbName"];
echo $POLLDB["host"];
echo $POLLDB["user"];
echo $POLLDB["pass"];
echo $POLLDB["class"];
*/
class polldb_sql {
    var $conn_id;
    var $result;
    var $record;
    var $db;
    var $port;
    var $query_count;

    function polldb_sql() {
        global $POLLDB;
        $this->query_count=0;
        $this->db = $POLLDB;
		if(ereg(":",$this->db['host'])) {
            list($host,$port) = explode(":",$this->db['host']);
            $this->port = $port;
        } else {
            $this->port = 3306;
        }
    }

    function connect() {
        $this->conn_id = mysql_connect($this->db['host'].":".$this->port,$this->db['user'],$this->db['pass']); 
        if ($this->conn_id == 0) {
            $this->sql_error("Connection Error, kan geen verbinding met DB maken");
        }
        if (!mysql_select_db($this->db['dbName'], $this->conn_id)) {
            $this->sql_error("Database Error");
        }
        return $this->conn_id;
    }

    function query($query_string) {
        $this->result = mysql_query($query_string,$this->conn_id);
        $this->query_count++;
        if (!$this->result) {
            $this->sql_error("Query Error");
        }
        return $this->result;
    }

    function fetch_array($query_id) {
        $this->record = mysql_fetch_array($query_id,MYSQL_ASSOC);
        return $this->record;
    }

    function num_rows($query_id) {
        return ($query_id) ? mysql_num_rows($query_id) : 0;
    }

    function num_fields($query_id) {
        return ($query_id) ? mysql_num_fields($query_id) : 0;
    }

    function free_result($query_id) {
        return mysql_free_result($query_id);
    }

    function affected_rows() {
        return mysql_affected_rows($this->conn_id);
    }

    function close_db() {
        if($this->conn_id) {
            return mysql_close($this->conn_id);
        } else {
            return false;
        }
    }

    function sql_error($message) {
        $description = mysql_error();
        $number = mysql_errno();
        $error ="MySQL Error : $message\n";
        $error.="Error Number: $number $description\n";
        $error.="Date        : ".date("D, F j, Y H:i:s")."\n";
        $error.="IP          : ".getenv("REMOTE_ADDR")."\n";
        $error.="Browser     : ".getenv("HTTP_USER_AGENT")."\n";
        $error.="Referer     : ".getenv("HTTP_REFERER")."\n";
        $error.="PHP Version : ".PHP_VERSION."\n";
        $error.="OS          : ".PHP_OS."\n";
        $error.="Server      : ".getenv("SERVER_SOFTWARE")."\n";
        $error.="Server Name : ".getenv("SERVER_NAME")."\n";
        $error.="Script Name : ".getenv("SCRIPT_NAME")."\n";
        echo "<b><font size=4 face=Arial>$message</font></b><hr>";
        echo "<pre>$error</pre>";
        exit();
    }

}

?>
Last edited by Gregor on Tue Mar 27, 2007 5:46 am, edited 1 time in total.
User avatar
Gregor
Power Poster
Power Poster
Posts: 1874
Joined: Thu Mar 23, 2006 9:25 am

Re: Connect to a db via a UDT

Post by Gregor »

Something funny occured (no, it's not working, yet ;) )

The code in the UDT is:

Code: Select all

global $gCms;
$db =& $gCms->GetDb();

/* path */ 
$poll_path = "/home/httpd/vhosts/uisge-beatha.eu/httpdocs/poll";

require_once $poll_path."/include/config.inc.php";
require_once $poll_path."/include/$POLLDB[class]";
require_once $poll_path."/include/class_poll.php";
$CLASS["db"] = new polldb_sql;
$CLASS["db"]->connect();

$php_poll = new poll();

/* the first poll */
echo $php_poll->poll_process(1);

$CLASS["db"]->close_db();
When viewing the page using http://www.uisge-beatha.eu/index.php?page=test it does not give an error (neither a poll), but when using the preview from page 'test', it gives "Database error".

???
Vin

Re: Connect to a db via a UDT

Post by Vin »

Try to uncomment the lines on the beginning:

Code: Select all

echo $POLLDB["dbName"];
echo $POLLDB["host"];
echo $POLLDB["user"];
echo $POLLDB["pass"];
echo $POLLDB["class"];
*/
to check the values.
P.S.: I'm unable to reach your server.
User avatar
Gregor
Power Poster
Power Poster
Posts: 1874
Joined: Thu Mar 23, 2006 9:25 am

Re: Connect to a db via a UDT

Post by Gregor »

Removed the /* */ (except for [pass] ;) ) It looks like the correct variables are passed on, however the connect() function does not recognize them.

I can reach my server. Strange ???
Locked

Return to “CMSMS Core”