Jump to content

Recommended Posts

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

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);

 

 

 

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

 

+ 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..

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.

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

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.