milanello72 Posted January 10, 2013 Share Posted January 10, 2013 Hello! I'm trying to learn OOP and I studying this example from phpacademy: http://www.youtube.com/watch?v=Ny4IJwyZKwo But this code has an error: [b]Notice[/b]: Undefined property: Database::$numRows in [b]C:\Program Files (x86)\EasyPHP-5.3.7.0\www\work\php academy\index.php[/b] on line [b]7[/b] No books! The line 7 in index.php is if ($db->numRows == 0) { The code from index.php require_once('classes/database.php'); $db=new Database('localhost', 'root', '', 'librarie'); $db->query("select titlu from carti"); if ($db->numRows == 0) { echo 'No books!'; } else { foreach ($db->rows() as $carte) { echo $carte['titlu'], '<br>'; } echo '<p>', $db->numRows(), ' carti </p>'; } $db->disconnect(); and the code from database.php class Database { protected $_link, $_result, $_numRows; public function __construct($server, $username, $password, $db) { $this->_link=mysql_connect($server, $username, $password); mysql_select_db($db, $this->_link); } public function disconnect() { mysql_close($this->_link); } public function query($sql) { $this->_result=mysql_query($sql, $this->_link); $this->_numRows=mysql_num_rows($this->_result); } public function numRows() { $this->_numRows; } public function rows() { $rows=array(); for ($x=0; $x < $this->_numRows(); $x++) { $rows[] = mysql_fetch_assoc($this->_result); } return $rows; } } So, what can I do? Thank you very much for your help! Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 10, 2013 Share Posted January 10, 2013 1. You have a property _numRows and a function numRows(). You don't have a property numRows. 2. If you're trying to call the function numRows() you're going to have a problem in that your function doesn't do anything. Try return; Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted January 10, 2013 Share Posted January 10, 2013 You have numRows() method not numRows. if ($db->numRows == 0) Quote Link to comment Share on other sites More sharing options...
milanello72 Posted January 10, 2013 Author Share Posted January 10, 2013 I have put return $this->_numRows; , but it is the same error... Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 10, 2013 Share Posted January 10, 2013 Did you read the entire reply I gave? Quote Link to comment Share on other sites More sharing options...
milanello72 Posted January 10, 2013 Author Share Posted January 10, 2013 I have put and if ($db->numRows() == 0) and it is Fatal error: Call to undefined method Database::_numRows() in C:\Program Files (x86)\EasyPHP-5.3.7.0\www\work\php academy\classes\database.php on line 27 Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 10, 2013 Share Posted January 10, 2013 That line of code is not causing that error. Try to do a little debugging on your own now. It tells you where the problem is and exactly what is wrong. Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted January 10, 2013 Share Posted January 10, 2013 Fatal error: ? What's your updated Database class? Quote Link to comment Share on other sites More sharing options...
milanello72 Posted January 10, 2013 Author Share Posted January 10, 2013 i have put in database.php class Database { protected $_link, $_result, $_numRows; public function __construct($server, $username, $password, $db) { $this->_link=mysql_connect($server, $username, $password); mysql_select_db($db, $this->_link); } public function disconnect() { mysql_close($this->_link); } public function query($sql) { $this->_result=mysql_query($sql, $this->_link); $this->_numRows=mysql_num_rows($this->_result); } public function numRows() { return $this->_numRows; } public function rows() { $rows=array(); for ($x=0; $x < $this->_numRows; $x++) { $rows[] = mysql_fetch_assoc($this->_result); } return $rows; } } This code works! thank you very much for your help! Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 10, 2013 Share Posted January 10, 2013 Good job. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.