到目前为止,我已经得到了下面的代码,在尝试更新、删除或选择语句时,它们工作得很好。但是,当我尝试使用插入时遇到问题。如果有人能指出正确的方向,我将不胜感激。
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();
最佳答案
从使用有效的插入语句开始可能会有所帮助。
VALUES ( a , b , c )
不是
VALUES a, b , c
此外,为什么 dickens 将准备好的插入与字符串替换结合起来?
你是说
$q->prep("blah blah blah VALUES ( :date , etc etc ) ");
$q->bind(":日期", $日期);
或类似的东西。使用这两种技术是荒谬的。
关于php - 面向对象的 MySQL 语句,PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/317029/