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
Share on other sites

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;

Link to comment
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.