so i'm trying to make one class (for mysqli operations) as much reusable for other classes (which use different tables and parameters) as possible. A fragment from main class:
class sql
{
private $sql;
public function __construct()
{
$this->sql = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
if ($this->sql->connect_errno > 0)
{
die('Unable to connect to database: ' .$this->sql->connect_error);
}
elseif (!$this->sql->set_charset("utf8"))
{
printf('Error loading character set utf8: ' .$this->sql->error);
}
}
public function newEntry($table, $entryString, $valueString, $typeString, $dataString)
{
$statement = $this->sql->prepare("INSERT INTO $table($entryString) VALUES ($valueString)");
$statement->bind_param($typeString, $dataString);
$statement->execute();
$statement->close();
$this->sql->close();
}
}
and here is my other class, in which i'm trying to pass variables to main function newEntry():
class user extends sql
{
protected $table = 'foo';
public function newUser($name, $surname)
{
$entryString = 'name, surname';
$valueString = '?, ?';
$typeString = 'ss';
$dataString = '$name, $surname';
parent::newEntry($this->table, $entryString, $valueString, $typeString, $dataString);
}
}
so when i'm trying to execute this, i get a "Warning: mysqli_stmt::bind_param(): Number of elements in type definition string doesn't match number of bind variables in..". I understand it's because my $dataString acts like a single entry: "John, Doe". Is there a way to make it act like numerous separate params instead of just a string?