Page 1 of 1

help me finish this mysqli class please

Posted: Sun Oct 11, 2009 1:06 pm
by dsick
i am trying to add a create method here and having a bit of trouble... this is the working code with just the read method....

Code: Select all

<?php
 
class database {
  
  public $mysql;
 
 
  function __construct() {
    $this->mysql = new mysqli('localhost', 'root','toot','dbname') or die('There was a problem connecting to the db');
  }
 
  function read($sql) {
    if(($result = $this->mysql->query($sql)) != NULL) {
      // echo $result->num_rows;
      while($row = $result->fetch_object()) {
        $x[] = $row->day;
        $x[] = $row->exercise;
        $x[] = $row->time;
      }
      return $x;
    }
    else {
      echo $this->mysql->error;
    }
    
  } //end query function 
  
  //creating the offer..... 
  function create($sql) {
  //create 
 
    
    
  }
 
  function __destruct() {
    // close out the database connection;
    $this->mysql->close();
  }
 
}



this is the code with the create method added... i thought i was doing it right it was working untill i got to the create method, should stmt and mysql be static?

Code: Select all

 
 
<?php
 
class database {
  
  public $mysql;
  public $stmt;
 
  function __construct() {
    $this->mysql = new mysqli('localhost', 'root','toot','dbname') or die('There was a problem connecting to the db');
  }
 
  function read($sql) {
    if(($result = $this->mysql->query($sql)) != NULL) {
      // echo $result->num_rows;
      while($row = $result->fetch_object()) {
        $x[] = $row->day;
        $x[] = $row->exercise;
        $x[] = $row->time;
      }
      return $x;
    }
    else {
      echo $this->mysql->error;
    }
    
  } //end query function 
  
  //creating the offer..... 
  function create($sql) {
    if($this->stmt = $this->mysql->prepare('INSERT INTO data VALUES (NULL,?,?,?)')) {
  $this->stmt->bind_param($sql);
  $this->stmt->execute();
  $this->stmt->close();
} else {
  echo 'error: ' . $this->mysql->error;
}
    
    
  }
 
  function __destruct() {
    // close out the database connection;
    $this->mysql->close();
  }
 
}
 
 
 

Re: help me finish this mysqli class please

Posted: Sun Oct 11, 2009 2:39 pm
by markusn00b
What is the exact problem you're having with it? It's much more helpful if you tell us what *is* happening, what *isn't* happening, what *should* be happening, what *shouldn't* be happening, etc.

Help us help you.

Mark.

Re: help me finish this mysqli class please

Posted: Sun Oct 11, 2009 3:36 pm
by dsick
well i deleted the index that i echo'd it out in

but it said unexpected $end

could it be because i have it closing twice.. once in the create method then again in the destruct method.

Re: help me finish this mysqli class please

Posted: Sun Oct 11, 2009 4:05 pm
by markusn00b
dsick wrote:well i deleted the index that i echo'd it out in

but it said unexpected $end

could it be because i have it closing twice.. once in the create method then again in the destruct method.
Unexpected $end.. are you sure? I can check the syntax of the code, and it shows no errors. But, yes, you should only close the connection in your destructor.

Re: help me finish this mysqli class please

Posted: Sun Oct 11, 2009 5:24 pm
by dsick
im not getting any errors after i fixed those.. and its still not inserting right.. checked my paren's to

im using php designer so it makes it hard to get them wrong..

edits.. got everything working..heres changes i made to create();

Code: Select all

 
//added this under public mysql
public $day;
 public $exercise;
public $time;
 
 
 
function create() {
    if($this->stmt= $this->mysql->prepare('INSERT INTO weightloss VALUES (NULL,?,?,?)')) {
  //change to form variables        
  $this->day = 1;
  $this->exercise = 2;
  $this->time = 4;      
  $this->stmt->bind_param('sss',$this->day,$this->exercise,$this->time);
  $this->stmt->execute();
} else {
  echo 'error: ' . $this->mysql->error;
}
    
    
  }
 
 
im going to pass in $sql as the params for create() so it can be reused :)