0

I am trying to make a 1 on 1 chat website by learning as I progress, but I've come to a hault.

I can't write to the database.

I have four php files linked below.

  • Index
  • Init:

    session_start();
    
    define('LOGGED_IN', true);
    
    require 'classes/Core.php';
    require 'classes/Chat.php';
    
    ?>
    
  • Chat

  • Core:

    class Core {
    protected $db, $result;
    private $rows;
    
    public function __construct() {
        $this->db = new mysqli("localhost","root","");
    }
    
    public function query($sql) {
        $this->result = $this->db->query($sql);
    }
    
    public function rows() {
        for($x = 1; $x <= $this->db->affected_rows; $x++) {
            $this->rows[] = $this->result->fetch_assoc();
        }
        return $this->rows;
    }
     }
    
    ?>
    

I have a MySql database set with WAMP.

P.S. Yes, I have opened the "< ? php" but it doesn't get displayed here.

1 Answers1

0

From what I have seen you do not select a default database. You must either give a default database in

$this->db = new mysqli("localhost","root","", "mydatabase");

or select one later with

$this->db->select_db("mydatabase");

You also don't check the return values of the mysql calls. For example, add

public function query($sql) {
    $this->result = $this->db->query($sql);
    if ($this->result === false) {
        echo $this->db->error;
    }
}

after your mysql statements, in order to see whether the statements succeed or fail.

For debugging purposes you can display the sql and corresponding result

public function query($sql) {
    var_dump($sql);
    $this->result = $this->db->query($sql);
    var_dump($this->result);
    echo $this->db->error;
}
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198