vic vance Posted January 7, 2014 Share Posted January 7, 2014 Hey I was trying out PDO, and I got a minor error: Fatal error: Call to undefined method connection::prepare() on line 12 This is my connect.php class <?php class connection { private $user; private $pass; public function __construct() { $this->user = "lala"; $this->pass = "28282"; } public function dbConnect() { try { $dbh = new PDO('mysql:host=localhost;dbname=project', $this->user, $this->pass); array(PDO::ATTR_PERSISTENT => true); } catch (PDOException $e) { print "<b>Connection Failed:</b> ".$e->getMessage().""; die(); } } } ?> This is the index.php <?php error_reporting(E_ALL ^ E_NOTICE); define( 'ROOT', "./" ); define( 'temp', "templates/" ); require_once 'connect.php'; $con = new connection(); $con->dbConnect(); $head = implode("", file(temp."head.html")); echo $head; $stmt = $con->prepare('SELECT * FROM db_members'); // error here LINE 12 $stmt->execute(); $result = $stmt->fetchAll(\PDO::FETCH_OBJ); foreach($result as $row){ print($row->username); } $foot = implode("", file(temp."/foot.html")); echo $foot; ?> I know I am connected because when I remove the connect.php class and try connecting directly it seems to work: $con = new PDO('mysql:host=localhost;dbname=project', 'lala', '28282'); // removed the class and directly connected $head = implode("", file(temp."head.html")); echo $head; $stmt = $con->prepare('SELECT * FROM db_members'); // error goes away and everything works when I directly connect $stmt->execute(); Am I calling the connect.php class wrong? Please help, Thank you. Link to comment https://forums.phpfreaks.com/topic/285181-slight-error-help/ Share on other sites More sharing options...
ginerjm Posted January 7, 2014 Share Posted January 7, 2014 1 - no need to complicate your life and make a class out of this. Just take that simple code and made it an include file for it. 2 - you created the var $con as an object of connection type. 3 - you executed dbconnect for that object. And - what? You have no pdo connection variable. See how the class made things so complex? $pdo = $con->dbConnect(); $stmt = $pdo->prepare(..... Tip - make the dbname an argument of your dbconnect function (which is all you need) and then you won't have to have a different one for every db you have. (Assuming you use the same master id/pswd for all your dbs) Link to comment https://forums.phpfreaks.com/topic/285181-slight-error-help/#findComment-1464333 Share on other sites More sharing options...
vic vance Posted January 7, 2014 Author Share Posted January 7, 2014 I get what you mean, and I was forgot a return. I changed this also: $con = new PDO('mysql:host=localhost;dbname=project', $this->user, $this->pass); return new PDO('mysql:host=localhost;dbname=project', $this->user, $this->pass); Thank you. Link to comment https://forums.phpfreaks.com/topic/285181-slight-error-help/#findComment-1464337 Share on other sites More sharing options...
ginerjm Posted January 7, 2014 Share Posted January 7, 2014 But you really should get rid of the class thing. Adds needless complication and overhead to your code. And it's not practical since you don't parameterize the dbname. require_once($php_path."/pdo_connect_select.php"); // contains PDOConnect function $pdo = PDOConnect("mydbname"); $q = "select * from table ...... "; $qst = $pdo->prepare($q); $qst->execute(); And the included/required file (pdo_connect_select.php) is: function PDOConnect($dbname) { $host="mysql:host=domain.com;dbname=$dbname;charset=utf8"; $uid = "myuser"; $pswd = "mypswd"; Try { $mysql = new PDO($host,$uid,$pswd); } catch (PDOException $e) { echo "Fatal Error<br>Failed to connect to mysql via PDO. PDO Error msg is:<br>".$e->getMessage(); return false; } if (!$mysql) { echo "Failed to connect to mysql via PDO. Error returned is: " . GetPDO_ErrorMsg($mysql); return false; } else return $mysql; } //***************************** function GetPDO_ErrorMsg($pdo,$i = 2) { $pdo_errinfo = $pdo->ErrorInfo(); return $pdo_errinfo[$i]; } Link to comment https://forums.phpfreaks.com/topic/285181-slight-error-help/#findComment-1464341 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.