Olli Posted October 7, 2011 Share Posted October 7, 2011 Hi, this php code not working for some reason class db { public function connect() { $yhteys = new PDO("mysql:host=*****;dbname=*****", "*******", "******"); } public function get($query, $parameters){ $prepare = $yhteys->prepare($query); $prepare->execute($parameters); $result = $prepare->fetch(); $count = $prepare->rowCount(); return array($result, $count); } } $db = new db(); $content = $db->get("SELECT * FROM tuotteet WHERE id = ?", array("1")); print $content; It gives error:Parse error: syntax error, unexpected T_OBJECT_OPERATOR Thank you for help! Quote Link to comment https://forums.phpfreaks.com/topic/248627-php-class-problem/ Share on other sites More sharing options...
Buddski Posted October 7, 2011 Share Posted October 7, 2011 $yhteys is only defined in the connect function. Quote Link to comment https://forums.phpfreaks.com/topic/248627-php-class-problem/#findComment-1276819 Share on other sites More sharing options...
Olli Posted October 7, 2011 Author Share Posted October 7, 2011 Ok... i´m new to classes, so how should I fix it....? Quote Link to comment https://forums.phpfreaks.com/topic/248627-php-class-problem/#findComment-1276821 Share on other sites More sharing options...
Buddski Posted October 7, 2011 Share Posted October 7, 2011 class db { var $yhteys; // could be private or public etc.. public function connect() { $this->yhteys = new PDO("mysql:host=*****;dbname=*****", "*******", "******"); } public function get($query, $parameters){ $prepare = $this->yhteys->prepare($query); $prepare->execute($parameters); $result = $prepare->fetch(); $count = $prepare->rowCount(); return array($result, $count); } } Quote Link to comment https://forums.phpfreaks.com/topic/248627-php-class-problem/#findComment-1276822 Share on other sites More sharing options...
Olli Posted October 7, 2011 Author Share Posted October 7, 2011 Still doesnt work $yht = new db(); $content = $yht->get("SELECT * FROM tuotteet WHERE id = ?", array("1")); print_r($content); Quote Link to comment https://forums.phpfreaks.com/topic/248627-php-class-problem/#findComment-1276827 Share on other sites More sharing options...
Buddski Posted October 7, 2011 Share Posted October 7, 2011 That only fixes one of the errors I saw, there may still be issues. Are you getting any errors? Still doesnt work is not an error. What are you expecting and what is actually happening? Quote Link to comment https://forums.phpfreaks.com/topic/248627-php-class-problem/#findComment-1276832 Share on other sites More sharing options...
Olli Posted October 7, 2011 Author Share Posted October 7, 2011 It prints nothing even though i have this error_reporting(E_ALL); ini_set('display_errors', '1'); And should I execute connect() somewher? Quote Link to comment https://forums.phpfreaks.com/topic/248627-php-class-problem/#findComment-1276837 Share on other sites More sharing options...
Buddski Posted October 7, 2011 Share Posted October 7, 2011 Do you know if the PDO is successful? public function connect() { try { $this->yhteys = new PDO("mysql:host=*****;dbname=*****", "*******", "******"); } catch (PDOException $e) { echo 'Could not connect: ' . $e->getMessage(); } } Quote Link to comment https://forums.phpfreaks.com/topic/248627-php-class-problem/#findComment-1276838 Share on other sites More sharing options...
Buddski Posted October 7, 2011 Share Posted October 7, 2011 And should I execute connect() somewher? Of course you should.. otherwise it will not connect.. A better solution would be to rename connect() to __construct() Quote Link to comment https://forums.phpfreaks.com/topic/248627-php-class-problem/#findComment-1276841 Share on other sites More sharing options...
Olli Posted October 7, 2011 Author Share Posted October 7, 2011 Now i got it to work. Thank you for helping ! If I have more questions I will ask here so i wont mark this solved Quote Link to comment https://forums.phpfreaks.com/topic/248627-php-class-problem/#findComment-1276842 Share on other sites More sharing options...
Olli Posted October 7, 2011 Author Share Posted October 7, 2011 Is this good way to do it? Or should I use just fetchAll ? class db { private $yhteys; function __construct() { try { $this->yhteys = new PDO("................"); } catch (PDOException $e) { echo 'Could not connect: ' . $e->getMessage(); } } private function send_query($query, $parameters, $method){ $prepare = $this->yhteys->prepare($query); $prepare->execute($parameters); if($method == "all"){ $result = $prepare->fetchAll(); } else { $result = $prepare->fetch(); } $count = $prepare->rowCount(); return array($result, $count); } public function get($query, $parameters){ $this->sendQuery($query, $parameters, "all"); } public function getAll($query, $parameters){ $this-sendQuery($query, $parameters, "fetch"); } } Quote Link to comment https://forums.phpfreaks.com/topic/248627-php-class-problem/#findComment-1276843 Share on other sites More sharing options...
Buddski Posted October 7, 2011 Share Posted October 7, 2011 That code will not work, (not to mention you have getAll() calling fetch and get() calling all) if you call get() it will run send_query() and return an array to get(), but get() does nothing with the array. You could re-write your get() to do it all. public function get($query, $parameters, $method = 'all'){ $prepare = $this->yhteys->prepare($query); $prepare->execute($parameters); if($method == "all"){ $result = $prepare->fetchAll(); } else { $result = $prepare->fetch(); } $count = $prepare->rowCount(); return array($result, $count); } With that code if you call get without the $method paramter it will automatically fetch all. Quote Link to comment https://forums.phpfreaks.com/topic/248627-php-class-problem/#findComment-1276851 Share on other sites More sharing options...
Olli Posted October 7, 2011 Author Share Posted October 7, 2011 yes but I try to avoid using too many params in functions. In my old function file (not made with classes) there is upto 8 seperate parameters in functions.... Quote Link to comment https://forums.phpfreaks.com/topic/248627-php-class-problem/#findComment-1276865 Share on other sites More sharing options...
Buddski Posted October 7, 2011 Share Posted October 7, 2011 3 isn't that many.. besides its an optional parameter. You would rather have 3 functions than 3 parameters? Having 3 parameters is a lot less code to worry about than having 3 functions, i think anyway.. Quote Link to comment https://forums.phpfreaks.com/topic/248627-php-class-problem/#findComment-1276868 Share on other sites More sharing options...
KevinM1 Posted October 7, 2011 Share Posted October 7, 2011 You could always pass in an array of parameters, too.... Quote Link to comment https://forums.phpfreaks.com/topic/248627-php-class-problem/#findComment-1276871 Share on other sites More sharing options...
Olli Posted October 7, 2011 Author Share Posted October 7, 2011 yes thats true. But now I have new question: Every time I call some class it does again the same construct function: class product { public function getId($from, $id){ switch($from){ case "seo": $query = "SELECT id FROM tuotteet WHERE seo = ?"; break; case "normal": $query = "SELECT id FROM tuotteet WHERE nimi = ?"; break; } $result = $db->get($query, array($id)); return $result[0]; } public function details($id){ $result = $db->get($query, array($id)); return $result; } } Example, if I need to use both of those functions in one page it creates $yhteys two times. Is that bad? Quote Link to comment https://forums.phpfreaks.com/topic/248627-php-class-problem/#findComment-1276872 Share on other sites More sharing options...
Buddski Posted October 7, 2011 Share Posted October 7, 2011 You should pass the created $db variable into any other class that needs it to function. Create a __construct() function for the product class aswell as a $db variable for in the class. function __construct($db) { $this->db = $db; } so now $p = new product(); should be called using $p = new product($db); As long as the db class is called BEFORE product. Once you have done this.. the query functions in the product class will have to be called like $result = $this->db->get($query, array($id)); Make sense? (There is a lot of db flying around in this post) Quote Link to comment https://forums.phpfreaks.com/topic/248627-php-class-problem/#findComment-1276883 Share on other sites More sharing options...
Olli Posted October 7, 2011 Author Share Posted October 7, 2011 Ok, i will try to understand. But how can I then call product->details ? Example print product->details("1"); How would I pass the $yht = new db(); there ? and yes I know I could do it like: $db = db(); $something = product($db); print $something->details("4"); But isn´t there any easier way? Quote Link to comment https://forums.phpfreaks.com/topic/248627-php-class-problem/#findComment-1276891 Share on other sites More sharing options...
Buddski Posted October 7, 2011 Share Posted October 7, 2011 That is the easy way.. Once you have passed in the DB into the product class (as I suggested) it will be accessable by all the function inside the product class. Having said that. You product class details() function does not have $query defined inside it. It might be a good idea to read this. Quote Link to comment https://forums.phpfreaks.com/topic/248627-php-class-problem/#findComment-1276903 Share on other sites More sharing options...
Olli Posted October 7, 2011 Author Share Posted October 7, 2011 Thank you for helping. Now I added query and it still doesnt work: class db { private $connection; function __construct() { try { $this->connection = new PDO("................."); } catch (PDOException $e) { echo 'Yhteysvirhe: ' . $e->getMessage(); } } public function get($query, $parameters, $method = 'all'){ $prepare = $this->connection->prepare($query); $prepare->execute($parameters); if($method == "all"){ $result = $prepare->fetchAll(); } else { $result = $prepare->fetch(); } $count = $prepare->rowCount(); return array($result, $count); } } class product { function __construct($db) { $this->db = $db; } public function getId($from, $id){ switch($from){ case "seo": $query = "SELECT * FROM tuotteet WHERE id = ?"; break; case "normal": $query = "SELECT * FROM tuotteet WHERE nimi = ?"; break; } $result = $this->$db->get($query, array($id)); return $result[0]; } public function details($id){ $query = "SELECT * FROM tuotteet WHERE id = ?"; $result = $this->$db->get($query, array($id)); return $result; } } and test file $yht = new db(); print product->details("1"); (no error message, just gives empty page) Quote Link to comment https://forums.phpfreaks.com/topic/248627-php-class-problem/#findComment-1276918 Share on other sites More sharing options...
Buddski Posted October 7, 2011 Share Posted October 7, 2011 You arent initiating the product class.. Quote Link to comment https://forums.phpfreaks.com/topic/248627-php-class-problem/#findComment-1276924 Share on other sites More sharing options...
Olli Posted October 7, 2011 Author Share Posted October 7, 2011 Hmm I forgot it,sorry Here ´s the new code and still gives empty page: $connection = new db(); $product = new product($connection); print $product->details("1"); Quote Link to comment https://forums.phpfreaks.com/topic/248627-php-class-problem/#findComment-1276928 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.