php-先行編譯

來源:互聯網
上載者:User
當出現當量操作sql語句,比如大量將資料插入資料庫中,原來的那種單個執行sql語句或者批量執行sql語句的做法,顯然是不可行的,因為無論是單個執行還是批量執行都會連續的發送sql語句向資料庫中,資料庫接到sql語句對它進行編譯處理,從而導致效率底下。

而php中出現的先行編譯解決了這個問題,他的工作原理是:將sql語句發過去,資料庫對這一個sql語句進行先行編譯處理。之後你只需要將要資料發送到資料庫即可。

下面通過一個官方的例子來說明這個情況:

prepare("INSERT INTO CountryLanguage VALUES (?, ?, ?, ?)");
$stmt->bind_param('sssd', $code, $language, $official, $percent);$code = 'DEU';$language = 'Bavarian';$official = "F";$percent = 11.2;/* execute prepared statement */$stmt->execute();printf("%d Row inserted.\n", $stmt->affected_rows);/* close statement and connection */$stmt->close();/* Clean up table CountryLanguage */$mysqli->query("DELETE FROM CountryLanguage WHERE Language='Bavarian'");printf("%d Row deleted.\n", $mysqli->affected_rows);/* close connection */$mysqli->close();?> 


其中$mysqli->prepare進行sql語句的先行編譯處理,

$stmt->bind_param是進行參數綁定。

$stmt->execute();是進行插入操作。

如果需要插入多個資料,只需要操作$stmt->bind_param和$stmt->execute();即可。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.