[英]Object Oriented MySQL Statements, PHP
到目前為止,我已經獲得了下面的代碼,在嘗試更新,刪除或選擇語句時,該代碼很有效。 但是,當我嘗試使用插入內容時遇到了問題。 如果有人能指出我正確的方向,我將不勝感激。
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
此外,為什么狄更斯將結合了字符串的置換插入物與插入物組合在一起?
你的意思是
$ q-> prep(“等等等等值(:date等));
$ q-> bind(“:date”,$ date);
或類似的規定。 使用這兩種技術都是荒謬的。
您可能也應該將values()包裝在括號中,例如:
$MyDB->prep("INSERT INTO `demo` (`id`, `name`, `score`, `dept`, `date`) VALUES ('1','James Kablammo', '1205550', 'Marketing', '$date'"));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.