Jump to content

Notes/blog class timing out on me/no error given


cgm225

Recommended Posts

I am having problems debugging this class. Currently, it "timesout" when implemented as I have shown below (no error message), but it I remove the while statement from the AllNotes method, then the script runs.

 

Any ideas what I am doing wrong?

 

$mysqli = new mysqli(MYSQL_SERVER,MYSQL_SERVER_USERNAME,MYSQL_SERVER_PASSWORD);

abstract class Notes {
    protected $connection;
    protected $data = array();

    protected function __construct(mysqli $connection) {
        $this->connection = $connection;
    }
   
    public function getData() {
        return $this->data;
    }
}

class OneNote extends Notes {
    private $entry;

    public function __construct(mysqli $connection, $id) {
        parent::__construct($connection);
        $this->entry = $id;
        $this->getEntry();
    }

    private function getEntry() {
        $query = "SELECT id, title, date, note, timestamp FROM notes WHERE id = ?";
        $statement = $this->connection->prepare($query);
        $statement->bind_param('s', $this->id);
        $statement->bind_result($id, $val1, $val2, $val3, $val4);
        $statement->execute();
        if($statement->fetch()) {
            $this->data = array(
                'id'   => $id,
                'col1' => $val1,
                'col2' => $val2,
                'col3' => $val3,
                'col4' => $val4
            );
        }
    }

    public function getData() {
        return $this->data;
    }
}

class AllNotes extends Notes {
    public function __construct(mysqli $connection) {
        parent::__construct($connection);
        $this->getEntry();
    }

    private function getEntry() {
        $query = "SELECT id, title, note, date, timestamp FROM notes ORDER BY id DESC";
        $statement = $this->connection->prepare($query);
        if($statement == false) {
          die($this->connection->error);
        }
        $statement->bind_result($id, $val1, $val2, $val3, $val4);
        $statement->execute();
        while($statement->fetch()) {
            $this->data[$id] = array( //Creating two dimensional array
                'id'   => $id,
                'col1' => $val1,
                'col2' => $val2,
                'col3' => $val3,
                'col4' => $val4
            );
        }
        return $this->data;
    }
}

$mysqli->select_db('example_db');
$notes = new AllNotes($mysqli);
$data = $notes->getData();
foreach($data as $note) {
   echo "<h3 class='title'>".htmlentities($note['title'])."</h3>\n";
   echo "<p class='date'>".htmlentities($note['date'])."</h3>\n";
   echo "<p class='note'>".nl2br(htmlentities($note['note']))."</h3>\n";
} 

I'm not sure about mysqli but your problem must lay here:

private function getEntry() {
        $query = "SELECT id, title, note, date, timestamp FROM notes ORDER BY id DESC";
        $statement = $this->connection->prepare($query);
        if($statement == false) {
          die($this->connection->error);
        }
        $statement->bind_result($id, $val1, $val2, $val3, $val4);
        $statement->execute();
        while($statement->fetch()) {
            $this->data[$id] = array( //Creating two dimensional array
                'id'   => $id,
                'col1' => $val1,
                'col2' => $val2,
                'col3' => $val3,
                'col4' => $val4
            );
        }
        return $this->data;
    }

Because it's the only while loop...

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.