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
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
Your variable working locally. You need to assign it in class level. Your code should be:
Then get to the info using this:
}
You need to first declare the class variables.
The way you were doing it, the scope of $city was only within the function, not stored as a field
do it
then
while loop in each iteration is updating info. so, u can echo in the while like
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
.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.