PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
<?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?
<?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();
}
}
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.
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.
//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