schubertgergo Posted August 2, 2013 Share Posted August 2, 2013 hi, here is my code: class DBCon{ private $con; private $host; private $user; private $password; private $dataBase; function __construct(){ $this->host = 'localhost'; $this->user = 'root'; $this->password = ''; $this->dataBase = 'import'; $this->con = new mysqli($this->host, $this->user,$this->password, $this->dataBase); } function connectionString(){ if($this->con->error){ echo "Sikertelen adatbázis kapcsolódás"; die(); } else{ return $this->con; } }} class showUser{ private userId; function __construct($id){ $this->userId=$id; } function showData(){ $q="select * from user where id=".$this->id; $r=$con->query($q); while($row=$r->fetch_assoc()){ echo $row['name']; } } } I dont know how to add the $con value to showuser class, and make the showdata() function work. Thank you for your help. Link to comment https://forums.phpfreaks.com/topic/280740-access-from-another-class/ Share on other sites More sharing options...
PravinS Posted August 2, 2013 Share Posted August 2, 2013 use inheritance refer: http://php.net/manual/en/keyword.extends.php Link to comment https://forums.phpfreaks.com/topic/280740-access-from-another-class/#findComment-1443079 Share on other sites More sharing options...
schubertgergo Posted August 2, 2013 Author Share Posted August 2, 2013 thank you, but i still have a problem with the query. Fatal error: Call to a member function fetch_assoc() on a non-object in C:\xampp\htdocs\import_\dbconn.php on line 50 here is the code: <?phpclass DBCon{ private $con; private $host; private $user; private $password; private $dataBase; function __construct() { $this->host = 'localhost'; $this->user = 'root'; $this->password = ''; $this->dataBase = 'importtable'; } function connectionString() { $this->con = new mysqli($this->host, $this->user, $this->password, $this->dataBase); if ($this->con->error) { echo "Sikertelen adatbázis kapcsolódás"; die(); } else { return $this->con; } }}class showUser extends DBCon{ private $userId; function __construct($id) { $this->userId = $id; } function showData() { $q = "select * from user where id=" . $this->userId; $r = $this->connectionString()->query($q); while ($row = $r->fetch_assoc()) { echo $row['name']; } }}$o=new showUser(1);$o->showData();?> Link to comment https://forums.phpfreaks.com/topic/280740-access-from-another-class/#findComment-1443090 Share on other sites More sharing options...
PravinS Posted August 2, 2013 Share Posted August 2, 2013 where have you defined fetch_assoc() function or is it mysql_fetch_assoc() function Link to comment https://forums.phpfreaks.com/topic/280740-access-from-another-class/#findComment-1443091 Share on other sites More sharing options...
schubertgergo Posted August 2, 2013 Author Share Posted August 2, 2013 mysqli has fetch_asssoc. i use mysqli connection not mysql connection Link to comment https://forums.phpfreaks.com/topic/280740-access-from-another-class/#findComment-1443092 Share on other sites More sharing options...
PravinS Posted August 2, 2013 Share Posted August 2, 2013 sorry about that function showData() { $q = "select * from user where id=" . $this->userId; $r = $this->connectionString(); $result = $r->query($q); while ($row = $result->fetch_assoc()) { echo $row['name']; } } try replacing your showdata() function by above function Link to comment https://forums.phpfreaks.com/topic/280740-access-from-another-class/#findComment-1443093 Share on other sites More sharing options...
schubertgergo Posted August 2, 2013 Author Share Posted August 2, 2013 Sorry not working... i changed:<?phpclass DataBase{ private $con; private $host; private $user; private $password; private $dataBase; private $queryString; private $result; private $row; function __construct() { $this->host = 'localhost'; $this->user = 'root'; $this->password = ''; $this->dataBase = 'importtable'; $this->con = new mysqli($this->host, $this->user, $this->password, $this->dataBase); } function connectionString() { if ($this->con->error) { echo "Sikertelen adatbázis kapcsolódás"; die(); } } function selectUser($query){ $this->queryString=mysqli_real_escape_string($this->con,$query); $this->result=$this->con->query($this->queryString); $this->row=$this->result->fetch_assoc(); echo $this->row['id']; }}class showUser extends DataBase{ private $userId; function __construct($id) { $this->userId = $id; } function showData() { if(filter_var($this->userId)){ $q = "select * from user where id=" . $this->userId; $this->selectUser($q); } else{ } }}$o= new showUser(1);$o->showData();?> now i have this messages: Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\import_\dbconn.php on line 32Fatal error: Call to a member function query() on a non-object in C:\xampp\htdocs\import_\dbconn.php on line 33 Link to comment https://forums.phpfreaks.com/topic/280740-access-from-another-class/#findComment-1443095 Share on other sites More sharing options...
PravinS Posted August 2, 2013 Share Posted August 2, 2013 i think you database connection is not working, echo the $this->con->connect_errno just below the connection Link to comment https://forums.phpfreaks.com/topic/280740-access-from-another-class/#findComment-1443097 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.