mysqli::stmt_init
(mysqli_stmt_init)初始化一個語句並返回一個與mysqli_stmt_prepare一起使用的對象
stmt_init()
/ mysqli_stmt_init()
函數初始化語句並返回適合mysqli_stmt_prepare()
使用的對象。
初始化一個語句並返回一個用於stmt_prepare()
的對象:
<?php $mysqli = new mysqli ( "localhost" , "my_user" , "my_password" , "my_db" ) ; if ( $mysqli -> connect_errno ) { echo "Failed to connect to MySQL: " . $mysqli -> connect_error ; exit ( ) ; } $city = "Sandnes" ; // 創建一個預處理語句 $stmt = $mysqli -> stmt_init ( ) ; if ( $stmt -> prepare ( "SELECT District FROM City WHERE Name=?" ) ) { // 綁定參數 $stmt -> bind_param ( "s" , $city ) ; // 執行查詢 $stmt -> execute ( ) ; // 綁定結果變量 $stmt -> bind_result ( $district ) ; // 獲取值 $stmt -> fetch ( ) ; printf ( "%s is in district %s" , $city , $district ) ; // 關閉語句 $stmt -> close ( ) ; } $mysqli -> close ( ) ; ?>
初始化一個語句並返回一個用於mysqli_stmt_prepare() 的對象:
<?php $con = mysqli_connect ( "localhost" , "my_user" , "my_password" , "my_db" ) ; if ( mysqli_connect_errno ( ) ) { echo "Failed to connect to MySQL: " . mysqli_connect_error ( ) ; exit ; } $city = "Sandnes" ; // 創建一個預處理語句 $stmt = mysqli_stmt_init ( $con ) ; if ( mysqli_stmt_prepare ( $stmt , "SELECT District FROM City WHERE Name=?" ) ) { // 綁定參數 mysqli_stmt_bind_param ( $stmt , "s" , $city ) ; // 執行查詢 mysqli_stmt_execute ( $stmt ) ; // 綁定結果變量 mysqli_stmt_bind_result ( $stmt , $district ) ; // 獲取值 mysqli_stmt_fetch ( $stmt ) ; printf ( "%s is in district %s" , $city , $district ) ; // 關閉語句 mysqli_stmt_close ( $stmt ) ; } mysqli_close ( $con ) ; ?>