美文网首页
PHP-Prepared Statement

PHP-Prepared Statement

作者: Emist | 来源:发表于2018-11-28 00:41 被阅读0次

Parameter types:

i-integer
d-double
s-string
b- BLOB (large files e.g., images that need to be sent in packets)

DB: Test

table: test
col1: ID
col2: name

$host="localhost";
$user="root";
$pwd="root";
$db="Test";

Procedural: Select

$conn=mysqli_connect($host,$user,$pwd,$db);
if (!$conn) {
    die("Connecting error!".mysqli_connect_error());
}
else{
    $sql="insert into test (ID,name) values (?,?)";
    $stmt=mysqli_prepare($conn,$sql);
    $id=5;
    $name="Tester-5";
    mysqli_stmt_bind_param($stmt,"is",$id,$name);
    mysqli_stmt_execute($stmt);
    echo mysqli_stmt_error($stmt);
    mysqli_stmt_close($stmt);
    
}
mysqli_close($conn);

Object-Oriented: Insert

$conn=new mysqli($host,$user,$pwd,$db);
if(mysqli_connect_errno()){
    printf("Connect error: %s\n",mysqli_connect_error());
}
else{
    $sql="select ID, name from test where ID=?";
    $stmt=$conn->prepare($sql);
    $id=2;
    $stmt->bind_param("i",$id);
    $stmt->bind_result($ID,$tester);
    $stmt->execute();
    $stmt->fetch();
    if ($stmt->error) {
        printf("Error:%s",$stmt->error);
    }
    $stmt->close();
    echo $ID." ".$tester;
}
$conn->close();

相关文章

网友评论

      本文标题:PHP-Prepared Statement

      本文链接:https://www.haomeiwen.com/subject/dbyoqqtx.html