2

the code is as follow:

Class userinfo {
    function fetchdatabyemail($email) {
        $result=mysql_query(" SELECT * FROM users WHERE email='$email'"); 
        while($row = mysql_fetch_array($result)) {
            $name = $row['name'];
            $num = $row['num'];
            $city = $row['city'];
        }
        $numrows= mysql_num_rows($result);    
    }
}

now to get the info I do this :

$info = new userinfo();
$info->fetchdatabyemail('[email protected]');  
echo $info->city; 

and it doesnt return the info. I think Im doing something wrong any ideas please

6 Answers 6

5

do it

public $numrows;
public function fetchDataByEmail($email) {
        $result=mysql_query(" SELECT * FROM users WHERE email='$email'"); 
        while($row = mysql_fetch_assoc($result)) {
        $fetch[] = $row;
        }
        $this->numrows = mysql_num_rows($result);  
        return $fetch;  
}

then

$info = new userinfo();
$detail = $info->fetchDataByEmail('[email protected]');  
print_r($detail); // return all result array
$info->numrows; // will return number of rows.
Sign up to request clarification or add additional context in comments.

Comments

2

Your variable working locally. You need to assign it in class level. Your code should be:

Class userinfo {

public $name,$city,$num,$numrows;

function fetchdatabyemail($email) {
$result=mysql_query(" SELECT * FROM users WHERE email='$email'"); 
while($row = mysql_fetch_array($result)) {
$this->name = $row['name'];
$this->num = $row['num'];
$this->city = $row['city'];
}
$this->numrows= mysql_num_rows($result);

}

Then get to the info using this:

$info = new userinfo();
$info->fetchdatabyemail('[email protected]');  
echo $info->city; 

}

Comments

1

You should have a private variable and getter/setter for it (this is the proper way, see code below). You could also declare $city as a public variable and access directly to it from the class' instance.

class userinfo
{
    private $city = '';

    public function getCity()
    {
        return $this->city;
    }

    public function fetchDataByEmail($email)
    {
        // Your code here
        $this->city = $row['city'];
    }
}

$info = new userinfo();
$info->fetchDataByEmail('[email protected]');
echo 'City: '.$this->getCity();

Comments

1

I think your problem is the scope/visbility of your variables think you need to declare them outside of the scope of the function:

http://www.php.net/manual/en/language.oop5.visibility.php

class userinfo {
    public $name;
    public $num;
    public $city;
    public $numrows;
    function fetchdatabyemail($email) {
        $result=mysql_query(" SELECT * FROM users WHERE email='$email'"); 
        while($row = mysql_fetch_array($result)) {
            this->$name = $row['name'];
            this->$num = $row['num'];
            this->$city = $row['city'];
        }
        this->$numrows= mysql_num_rows($result);
    }
}

Comments

0

You need to first declare the class variables.

class Userinfo {

    $city;

    // then declare the function

}

The way you were doing it, the scope of $city was only within the function, not stored as a field

1 Comment

needs to declare if its private or public or protected :)
0

while loop in each iteration is updating info. so, u can echo in the while like

function fetchdatabyemail($email) {
    $result=mysql_query(" SELECT * FROM users WHERE email='$email'"); 
    while($row = mysql_fetch_array($result)) {
       echo $row['city'];
    }

or can store values in an array which is globally declared in the class and than echo the array.

in your code you have $city declared with local function scope which is not accessible from class $info->city.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.