i working first time in php , mysql. have run issue mysqli , can't figure out doing wrong.
i getting error can view here: http://www.polancophp.com/polanco-oop.php or
fatal error: call member function fetch_assoc() on non-object in e:\hostingspaces\jacobspd\polancophp.com\wwwroot\polanco-oop.php on line 37 here code:
<?php class member { public $memberid; public $businessname; public $username; public $email; public $memberurl; public $addr1; public $addr2; public $city; public $state; public $country; public $postalcode; public $domain; public $firstname; public $lastname; public function __construct($inmemberid) { $this->get_data($inmemberid); } public function get_data($inmemberid){ $connect_str = new mysqli('xxx','xxx','xxx','xxx'); /* check connection */ if (mysqli_connect_errno()) { printf("connect failed: %s\n", mysqli_connect_error()); exit(); } $query_str = 'select * members memberid = $inmemberid'; $result = $connect_str->query($query_str); echo $result; while (($row = $result->fetch_assoc()) !== null) { $businessname = $row["businessname"]; $username = $row["username"]; $email = $row["loginemail"]; $memberurl = ""; $addr1 = $row["address1"]; $addr2 = $row["address2"]; $city = $row["city"]; $state = $row["state"]; $country = $row["country"]; $postalcode = $row["postcode"]; $domain = "polancophp.com"; $firstname = $row["firstname"]; $lastname = $row["lastname"]; } /* close connection */ mysqli_close($connect_str); } public function get_member_url(){ echo $this->domain.'/'.$this->memberurl; } public function get_member_email(){ echo $this->email; } public function get_member_address(){ echo $this->addr1.' '.$this->city.', '.$this->state.' '.$this->country.' '.$this->postalcode; } public function get_member_fullname(){ echo $this->firstname.' '.$this->lastname; } } $member = new member(2); ?> <!doctype html public "-//w3c//dtd xhtml 1.0 strict//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>polanco's first object orientated programming</title> <link rel="stylesheet" type="text/css" href="common.css" /> <style type="text/css"> .pll {padding-left:10px} </style> </head> <body> <h1>member details</h1> <table cellspacing="0" cellpadding="0" border="0"> <tr> <td>name:</td> <td class="pll"><?php $member->get_member_fullname(); ?></td> </tr> <tr> <td>email:</td> <td class="pll"><?php $member->get_member_email(); ?></td> </tr> <tr> <td>profile url:</td> <td class="pll"><a href="<?php $member->get_member_url(); ?>"><?php $member->get_member_url(); ?></a></td> </tr> <tr> <td>profile address:</td> <td class="pll"><?php $member->get_member_address(); ?></td> </tr> </table> </body> </html> any appreciated.
while (($row = $result->fetch_assoc()) !== null) { $businessname = $row["businessname"]; when this, you're assigning database result local variable called $businessname; that's not same class's attribute. need assign as:
while (($row = $result->fetch_assoc()) !== null) { $this->businessname = $row["businessname"]; also - you're regenerating database connection inside get_data; more efficient create connection when create object, , assign connection variable private variable in class, can reference same connection every time run query.
Comments
Post a Comment