Object oriented statements MySQL, PHP
So far I have a code that works great when trying to update, delete, or select an operator. However, I am running into problems when trying to use insert. If someone could point me in the right direction, I would be grateful.
private function escape($value)
{
if(get_magic_quotes_gpc())
$value = stripslashes($value);
return mysql_real_escape_string($value, $this->dbConn);
}
/**
* Handles connection to the database.
* Die functions are used to catch any errors.
*/
public function connect($dbHost, $dbName, $dbUser, $dbPass)
{
$this->dbConn = mysql_connect(
$dbHost,
$dbUser,
$dbPass
) or die(mysql_error());
mysql_select_db($dbName, $this->dbConn) or die(mysql_error());
}
/**
* Loads a raw SQL string into the object $dbSql variable
*/
public function prep($sql)
{
$this->dbSql = $sql;
}
/**
* Load bound hooks and values into object variable
*/
public function bind($hook, $value)
{
$this->dbBind[$hook] = $this->escape($value);
}
/**
* Runs the SQL string in $dbSql object variable
*/
public function run()
{
$sql = $this->dbSql;
if(is_array($this->dbBind))
foreach($this->dbBind as $hook => $value)
$sql = str_replace($hook, "'" . $value . "'", $sql);
$this->dbQuery = mysql_query($sql) or die(mysql_error());
$this->dbBind = array();
return $this->numRows();
}
// Load SQL statment into object
$MyDB->prep("INSERT INTO `demo` (`id`, `name`, `score`, `dept`, `date`) VALUES '1','James Kablammo', '1205550', 'Marketing', '$date'");
// Bind a value to our :id hook
// Produces: SELECT * FROM demo_table WHERE id = '23'
$MyDB->bind(':id',1);
// Run the query
$MyDB->run();
0
source to share
2 answers
This can help you start by using valid insert operators.
VALUES ( a , b , c )
Not
VALUES a, b , c
Also, why are you having funny insertion with row combinations?
you mean
$ q-> prep ("blah blah blah VALUES (: date, etc., etc.)").
$ q-> bind (": date", $ date);
or something like that. using both methods is simply pointless.
+4
source to share