Jump to content

A problem in OOP


milanello72

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/272971-a-problem-in-oop/
Share on other sites

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!

Link to comment
https://forums.phpfreaks.com/topic/272971-a-problem-in-oop/#findComment-1404781
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.