PeterPans Posted June 6, 2012 Share Posted June 6, 2012 Hello, I have written a script in order to store and retrieve data from a database. The script used to work until i had to re-install lampp from the scratch. After the installation i had to do a lot of debugging and change the code a bit in order to overcome most errors and get closer to a working state. Here is the script. <?php class dataManipulation { public $name; public $text; public $email; function _constructor(){ $this->host = "localhost"; $this->uname = "root"; $this->pword = "1111"; $this->dbname = "testStorage"; $this->dbtable = "userData"; } function retrData(){ $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) or die(mysql_error()); //echo "<br/>" . $sql_query; while ($list = mysql_fetch_assoc($result)) { echo $list['name']; echo $list['email']; echo $list['text']; echo "<br/>"; } mysql_close($sql_query); } function storeData(){ $this->dbConnect = mysql_connect($this->host, $this->uname, $this->pword); if (!$this->dbConnect) die("Could not connect " . mysql_error()); mysql_select_db(`$this->dbname`); $sql_query = "INSERT INTO `$this->dbtable` VALUES('$name', '$email', '$text')"; $result = mysql_query($sql_query); if ($result){ echo "Data storage succesful <br/> . $result"; } else { echo "Error <br/>" . mysql_error(); } mysql_close($sql_query); } } ?> the problem in both functions is that they cannot see the values inside the variables $this->dbname and $this->dbtable, therefore i get an error "Invalid table name" or if I move those two variables from the constructor into each function separately I get another error "No database found". Now if I switch the variables with the values necessary the script works but it does not actually store anything in the database. The database is empty. + note that the " ` " symbol is used around the table and database variables, otherwise I would get a mysql syntax error.. Anybody knows what is wrong here? Any help would much appreciated. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/263762-storeretrieve-data-script-problem/ Share on other sites More sharing options...
PFMaBiSmAd Posted June 6, 2012 Share Posted June 6, 2012 Its - __construct() with two under-scores and the word construct Quote Link to comment https://forums.phpfreaks.com/topic/263762-storeretrieve-data-script-problem/#findComment-1351640 Share on other sites More sharing options...
PeterPans Posted June 6, 2012 Author Share Posted June 6, 2012 nice thank you.. both functions work now.. but not properly.. they seem to store and retrieve data from the DB but the storeData function does not actually work as the DB is empty.. any ideas why is that? Quote Link to comment https://forums.phpfreaks.com/topic/263762-storeretrieve-data-script-problem/#findComment-1351655 Share on other sites More sharing options...
PFMaBiSmAd Posted June 6, 2012 Share Posted June 6, 2012 You need to set php's error_reporting to E_ALL (or ever better a -1) and display_errors to ON in your master php.ini to get php to help you by reporting and displaying all the errors it detects. Stop and start your web server to get any change made to the master php.ini to take effect. Your $name, $email and $text variables inside the storeData() function/method are local to that function only. You would be getting undefined error messages for each of them. If your intent is to use the class variables by those same names, you would need to use $this->name, $this->email, and $this->text. Quote Link to comment https://forums.phpfreaks.com/topic/263762-storeretrieve-data-script-problem/#findComment-1351719 Share on other sites More sharing options...
PeterPans Posted June 6, 2012 Author Share Posted June 6, 2012 ok i have found the problem.. it was in the storeData object.. i should do this storeData($name, $email, $text) instead of this storeData() in order to parse the values of those variables in the function.. damn it.. stupid mistakes.. now it stores and retrieves the data found in the DB as expected.. Thanks a lot PFMaBiSmAd for helping me out.. I really appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/263762-storeretrieve-data-script-problem/#findComment-1351736 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.