PeterPans Posted April 2, 2012 Share Posted April 2, 2012 Hello, I am writing a data retrieval script, in order to print data stored in a DB. For some reason the script does not work.. More details below.. the form that calls the initiator script.. <form method='get' action="init.Get.php"> <div><input type="submit" name="get" value="See Data"></div> </form> the initiator script <?php include 'dataGet.class.php'; $data= new getData(); $data= getData($name, $email, $text); ?> my class.. <?php class getData { private $name; private $email; private $text; function __construct(){ $this->host = 'localhost'; $this->uname = 'root'; $this->pword = '1111'; $this->dbname = 'teststorage'; $this->dbtable = 'userData'; } function getData($name, $email, $text){ $this->dbconnect = mysql_connect($this->host, $this->uname, $this->pword); if (!$this->dbconnect) die("Connection Unable " . mysql_error()); mysql_select_db($this->dbname); $sql_query = "SELECT * FROM $this->dbtable "; $result = mysql_query($sql_query); if ($result){ echo $result; } else{ echo "Retrieval Unsuccesful"; } mysql_close($sql_query); } } ?> Can someone please tell me what am I doing wrong? Thank you in advance. Link to comment https://forums.phpfreaks.com/topic/260207-oop-php-data-retrieval-script-issue/ Share on other sites More sharing options...
batwimp Posted April 2, 2012 Share Posted April 2, 2012 You shouldn't have both a constructor function: __construct() and a method with the name of the class: function getData() in the same class. Naming a function with the name of a class is a PHP 4 thing, and was used as a constructor. Due to backward-compatibility issues, you might have problems with that. Gurus, please tell me if I'm wrong. Also, when you define a variable and set it to an instance of a class, you should also use this to send parameters, like so: $data = new getData($name, $email, $text); Link to comment https://forums.phpfreaks.com/topic/260207-oop-php-data-retrieval-script-issue/#findComment-1333667 Share on other sites More sharing options...
PeterPans Posted April 2, 2012 Author Share Posted April 2, 2012 I don't think that the answers you provided are correct.. i actually tried all of the suggestions you made but still it does not work.. + as far as i know this is not necessary $data = new getData($name, $email, $text); i have already defined them on the third line of the Initiator script, if you see.. Link to comment https://forums.phpfreaks.com/topic/260207-oop-php-data-retrieval-script-issue/#findComment-1333676 Share on other sites More sharing options...
KevinM1 Posted April 2, 2012 Share Posted April 2, 2012 First, like batwimp said, don't have a constructor and a function with the same name as your class. Second, you can't echo a mysql result directly. You need to fetch the data from the result. Rewritten class: class getData { private $name; private $email; private $text; // bad idea to just hard code these in private $host = 'localhost'; private $uname = 'root'; private $pword = '1111'; private $dbname = 'teststorage'; private $dbtable = 'userData'; function returnData($name, $email, $text){ $this->dbconnect = mysql_connect($this->host, $this->uname, $this->pword); if (!$this->dbconnect) die("Connection Unable " . mysql_error()); mysql_select_db($this->dbname); $sql_query = "SELECT * FROM $this->dbtable "; $result = mysql_query($sql_query); return $result; mysql_close($sql_query); } } $dataAccess = new getData(); $results = $dataAccess->returnData($name, $email, $text); if ($results) { while($row = mysql_fetch_assoc($results)) { // echo the results row-by-row } } else { echo "Could not retrieve data."; } Not the ideal solution, as it relies on hard coded info, but it should give you an idea of how to go about it. Note that there isn't a dedicated constructor method. I figured that since you're hard coding the db info, there wasn't a need to make a dedicated constructor for it. You'll need to do further testing on this, as I wrote it off the top of my head. Link to comment https://forums.phpfreaks.com/topic/260207-oop-php-data-retrieval-script-issue/#findComment-1333691 Share on other sites More sharing options...
batwimp Posted April 2, 2012 Share Posted April 2, 2012 Mu suggestion was just a starting point. It would be difficult to keep troubleshooting until fundamental issues are fixed. Link to comment https://forums.phpfreaks.com/topic/260207-oop-php-data-retrieval-script-issue/#findComment-1333694 Share on other sites More sharing options...
PeterPans Posted April 2, 2012 Author Share Posted April 2, 2012 thanks a lot for your help guys.. but i have fixed.. made some stupid mistakes.. don't know why i couldn't see them.. :/ anyway.. now it works so.. Thanks again! Link to comment https://forums.phpfreaks.com/topic/260207-oop-php-data-retrieval-script-issue/#findComment-1333701 Share on other sites More sharing options...
PeterPans Posted April 2, 2012 Author Share Posted April 2, 2012 ok so the most important mistake was this <?php include 'dataGet.class.php'; $data= new getData(); $data= getData($name, $email, $text); ?> which should have been.. <?php include 'dataGet.class.php'; $data= new getData(); $data->retrData($name, $email, $text); ?> now both scripts work.. i just get an sql error now.. but i know how to fix it.. Link to comment https://forums.phpfreaks.com/topic/260207-oop-php-data-retrieval-script-issue/#findComment-1333705 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.