2

代码如下:

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

现在获取我这样做的信息:

$info = new userinfo();
$info->fetchdatabyemail('email@email.com');  
echo $info->city; 

它不返回信息。我想我做错了任何想法请

4

6 回答 6

5

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

然后

$info = new userinfo();
$detail = $info->fetchDataByEmail('email@email.com');  
print_r($detail); // return all result array
$info->numrows; // will return number of rows.
于 2012-08-31T05:18:56.007 回答
2

您的变量在本地工作。您需要在班级级别分配它。您的代码应该是:

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

}

然后使用以下方式获取信息:

$info = new userinfo();
$info->fetchdatabyemail('email@email.com');  
echo $info->city; 

}

于 2012-08-31T05:18:09.443 回答
1

我认为您的问题是变量的范围/可见性认为您需要在函数范围之外声明它们:

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);
    }
}
于 2012-08-31T05:16:23.647 回答
1

您应该有一个私有变量和它的 getter/setter(这是正确的方法,请参见下面的代码)。您还可以将 $city 声明为公共变量并从类的实例直接访问它。

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();
于 2012-08-31T05:19:38.023 回答
0

您需要首先声明类变量。

class Userinfo {

    $city;

    // then declare the function

}

你这样做的方式,$city 的范围仅在函数内,而不是存储为字段

于 2012-08-31T05:19:10.130 回答
0

每次迭代中的 while 循环都在更新信息。所以,你可以像

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

或者可以将值存储在一个数组中,该数组在类中全局声明,而不是echo数组。

在您的代码中,您$city声明了不能从 class 访问的本地函数范围$info->city

于 2012-08-31T05:24:32.603 回答