php

mysqli 用法 stmt

日期:2016-12-08 阅读:1359

dmandwp系统 - wordpress系统和DM系统区块建站>>

http://www.php.net/mysqli

http://php.net/manual/en/mysqli-stmt.prepare.php

绑定结果给变量。 http://php.net/manual/en/mysqli-stmt.bind-result.php

mysqli::$insert_id — Returns the auto generated id used in the latest query  --返回当前插入id

/* prepare statement */
if ($stmt = $mysqli->prepare("SELECT Code, Name FROM Country ORDER BY Name LIMIT 5")) {
    $stmt->execute();

    /* bind variables to prepared statement */
    $stmt->bind_result($col1, $col2);

    /* fetch values */
    while ($stmt->fetch()) {  //--这是循环。如果只取一条,只要 $stmt->fetch 
 
        printf("%s %s\n", $col1, $col2);
    }

    /* close statement */
    $stmt->close();
}
/* close connection */
$mysqli->close();

多少条记录:http://php.net/manual/en/mysqli-stmt.num-rows.php  -- select

影响了多少条:  http://php.net/manual/en/mysqli-stmt.affected-rows.php  -- insert, delete, update

 

function getuserid($openid){      
       $returnV ='';
        $mysqli = creatmsyqlconnect();
         $stmt =  $mysqli->stmt_init();
    if ($stmt->prepare('SELECT id  FROM sessions WHERE openid = ?'))
   {
        $stmt->bind_param('s', $openid);
        $stmt->execute();  
         $stmt->store_result();//思路,先判断是否有记录。
        $num =  $stmt->num_rows;
         if($num=='0') $returnV = 'noresult'; //no result
         else{
             $stmt->bind_result($returnV);
             $stmt->fetch();
         }

      
        $stmt->close();
    }

    $mysqli->close();
    return $returnV;
   
}

 

<<点击返回