3 回答

TA貢獻1834條經驗 獲得超8個贊
$dbPreparedStatement = $db->prepare('INSERT INTO table (htmlcontent) VALUES (?)');$dbPreparedStatement->execute(array($yourHtmlData));
$dbPreparedStatement = $db->prepare('INSERT INTO table (htmlcontent) VALUES (:htmlcontent)');$dbPreparedStatement-> execute(array(':htmlcontent' => $yourHtmlData));
bindParam
$db->bindParam(':userId', $userId, PDO::PARAM_INT);
$dbPreparedStatement = $db->prepare('INSERT INTO table (postId, htmlcontent) VALUES (:postid, :htmlcontent)'); $dbPreparedStatement->bindParam(':postid', $userId, PDO::PARAM_INT);$dbPreparedStatement->bindParam(':htmlcontent', $yourHtmlData, PDO::PARAM_STR);$dbPreparedStatement->execute();
$db

TA貢獻1982條經驗 獲得超2個贊
構建一個通常的查詢方式,使其看起來與SQL控制臺中運行的SQL查詢完全一樣。 要做到這一點,一個人應該明白 一整套規則,而不僅僅是“使用MySQL_REAL_EXECH_String”。 規則,如: 字符串應該用引號和轉義來括起來。這是轉義的唯一含義:它只是逃避分隔符!(以及其他一些字符-字符串終止字符和轉義字符本身)。沒有周圍的引號,MySQL_REAL_EXECH_String就沒用了。 數字應該顯式地轉換為它的類型。雖然數據數字可以像字符串一樣受到威脅,但也有一些數字,比如限制子句參數,它們不能轉義,只能進行強制轉換。 發送查詢和數據 分別.這是最可取的方式,因為它可以縮短為只是“使用綁定”。所有字符串、數字和限制參數都可以綁定-完全不用擔心。 使用此方法,您的查詢將占位符按原樣發送到數據庫,綁定數據以單獨的數據包發送,因此不會發生干擾。就像 電碼和 數據分離。將程序(查詢本身)與數據分開發送。
但!
$orders = array("name","price","qty"); //field names$key = array_search($_GET['sort'],$orders)); // see if we have such a name$orderby = $orders[$key]; //if not, first one will be set automatically. smart enuf :)$query = "SELECT * FROM `table` ORDER BY $orderby"; //value is safe
$w = array();$where = '';if (!empty($_GET['rooms'])) $w[]="rooms='".mesc($_GET['rooms'])."'";if (!empty($_GET['space'])) $w[]="space='".mesc($_GET['space'])."'";if (!empty($_GET['max_price'])) $w[]="price < '".mesc($_GET['max_price'])."'";if (count($w)) $where="WHERE ".implode(' AND ',$w);$query="select * from table $where";
- 3 回答
- 0 關注
- 947 瀏覽
添加回答
舉報