Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

return values from class php oop

Tags:

oop

php

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

like image 754
cppit Avatar asked Mar 16 '26 00:03

cppit


2 Answers

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.
like image 91
diEcho Avatar answered Mar 18 '26 12:03

diEcho


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; 

}

like image 28
Chanon Avatar answered Mar 18 '26 14:03

Chanon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!