pienez Posted April 9, 2008 Share Posted April 9, 2008 Hello, I am having trouble with making connection to my database. I mean, I am using PEAR for the templates and if I want to make a connection to the database it will work in one function but it won't work in a second function on the same page. I have different php files for connection en sql statements, wich I than require_once in my function I need te have the data in. this is the "headfunction" where I call the two functions. function getData(){ require_once('HTML/Template/IT.php'); $template = new HTML_TEMPLATE_IT('templates'); $template->loadTemplatefile('interesse1.tpl'); // calling the first function (this works if I leaf second funtion out) $arrDub = alleCat(); // second function (this won't work) $arrWaCat = alleWaCat(); $template->parse(); return $template->get(); } these are the two functions // first function function alleCat(){ require_once('dbacties/connect.php'); require_once('dbacties/selectCat.php'); include_once('klassen/Categorie.php'); $arrCat = array(); while (($row = $res->fetchRow(MDB2_FETCHMODE_ASSOC))) { $cat = new Categorie(); $cat->setID($row['id']); $cat->setNaamNL($row['naamnl']); $cat->setBeschrijvingNL($row['beschrijvingnl']); $cat->setNaamEN($row['naamen']); $cat->setBeschrijvingEN($row['beschrijvingen']); array_push($arrCat,$cat); } return $arrCat } //second function function alleWaCat(){ require_once('dbacties/connect.php'); require_once('dbacties/selectWaCat.php'); include_once('klassen/WaardeCat.php'); $arrWaCat = array(); while (($row = $res->fetchRow(MDB2_FETCHMODE_ASSOC))) { $wa = new WaardeCat(); $wa->setID($row['id']); $wa->setCijfer($row['cijfer']); $wa->setUitleg($row['uitleg']); array_push($arrWaCat,$wa); } return $arrWaCat; } this is my connect.php <?php require_once('MDB2.php'); $dsn = "mysql://root:test1234@localhost/kul_db"; $mdb2 = MDB2::connect($dsn, TRUE); if (PEAR::isError($mdb2)) { die($mdb2->getMessage()); } ?> this is seleCat.php <?php $res = $mdb2->query('SELECT * FROM kul_db.tbl_categorie t'); if (PEAR::isError($res)) { die($res->getMessage()); }else{ return $res; } ?> this is selectWaCat.php <?php $res = $mdb2->query('SELECT * FROM kul_db.tbl_waarde_categorie t'); if (PEAR::isError($res)) { die($res->getMessage()); }else{ return $res; } ?> I think it has something to do with the 2 times require_once for the connection.php, but I dont know how to do it else. this is the error I get. Fatal error: Call to a member function query() on a non-object in C:\wamp\www\tripplanner\dbacties\selectCat.php on line 5 Link to comment https://forums.phpfreaks.com/topic/100283-solved-pear-connection-problem/ Share on other sites More sharing options...
pienez Posted April 9, 2008 Author Share Posted April 9, 2008 For all you guys who were wondering. I fixed it. the problem was, that I didnt put mdb2 global and because it was in a separete file and function it didnt recoginize it. Link to comment https://forums.phpfreaks.com/topic/100283-solved-pear-connection-problem/#findComment-512793 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.