$mysqli = new mysqli('localhost', 'user', 'pwd', 'dbname'); $query = "select goods_id,goods_name from sw_goods where goods_name=? "; $stmt = $mysqli->stmt_init(); //不直接用mysqli->query 是爲了使用參數綁定。參數綁定可避免SQL注入 $stmt->prepare($query); $name = '美圖手機'; $stmt->bind_param('s', $name); //有參數的查詢用綁定,可避免SQL注入 $stmt->execute(); $result = $stmt->get_result(); //mysqli_result(object) var_dump($result); var_dump($result->fetch_all()); $result->free(); //釋放結果集所請求的內存。在獲取龐大結果集時頗有用。
$mysqli->close(); //關閉數據庫
結果:mysql