php - Displaying array content from function? -
can tell me how display value of next code below?
in user.php file have next content:
class user { protected $userid; protected $useremail; protected $userpassword; public function __construct() { $this->userid = preg_replace('#[^0-9]#i', '', $_session['user_id']); $this->useremail = preg_replace('#[^a-za-z0-9@_.-]#i', '', $_session['user']); $this->userpassword = preg_replace('#[^a-za-z0-9]#i', '', $_session['user_password']); } public function userinfoquery() { $sql = "select * users id = '$this->userid' , email = '$this->useremail' , password = '$this->userpassword' limit 1"; $res = mysql_query($sql) or die(mysql_error()); $usermatch = mysql_numrows($res); if ($usermatch == 1) { while($row = mysql_fetch_array($res)) { $userdata = array( $userfirstname = $row['firstname'], $userlastname = $row['lastname'], $userbirthdate = $row['birthdate'], $usersex = $row['sex'], $useremail = $row['email'], $usercountry = $row['country'], $userregion = $row['region']); } } return $userdata; } }
in index php file when try:
$user = new user(); print_r($user->userinfoquery());
i have next results:
array ( [0] => firstname [1] => lastname [2] => 1990-11-23 [3] => male [4] => mail [5] => srbija [6] => town )
how can echo first , last names?
this:
array($userfirstname = $row['firstname'])
assigns value of $row['firstname']
variable $userfirstname
, puts result of assignment (the value of $row['firstname']
) array. it's same writing:
$userfirstname = $row['firstname']; array($row['firstname']);
to declare array key userfirstname
, need write:
array('userfirstname' => $row['firstname'])
from here, have normal array can access:
$userinfo = $user->userinfoquery(); echo $userinfo['userfirstname'];
this seem clunky though, , honestly, you're not using objects here. should save data queried database properties of object, use getters access properties 1 one or together. how design proper object little beyond scope/point of answer though.
Comments
Post a Comment