return values from class php oop

2019-04-02 09:50发布

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@email.com');  
echo $info->city; 

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

标签: php oop
6条回答
\"骚年 ilove
2楼-- · 2019-04-02 10:12

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);
    }
}
查看更多
霸刀☆藐视天下
3楼-- · 2019-04-02 10:16

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@email.com');  
echo $info->city; 

}

查看更多
Deceive 欺骗
4楼-- · 2019-04-02 10:21

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

查看更多
Summer. ? 凉城
5楼-- · 2019-04-02 10:31

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@email.com');  
print_r($detail); // return all result array
$info->numrows; // will return number of rows.
查看更多
地球回转人心会变
6楼-- · 2019-04-02 10:32

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.

查看更多
闹够了就滚
7楼-- · 2019-04-02 10:35

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('someone@example.com');
echo 'City: '.$this->getCity();
查看更多
登录 后发表回答