Jump to content

store/retrieve data script problem


PeterPans

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.. :P 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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.