help me finish this mysqli class please
Posted: Sun Oct 11, 2009 1:06 pm
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....
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;
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();
}
}