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();
网友评论