Sample code for bind_param () function usage in php

Source: Internet
Author: User
This article mainly introduces the bind_param () function usage in php, and briefly analyzes the functions, parameters, usage methods, and related precautions of the bind_param () function, for more information about bind_param () function usage in php, this article briefly analyzes the functions, parameters, usage methods, and precautions of the bind_param () function, for more information, see

This example describes the usage of the bind_param () function in php. We will share this with you for your reference. The details are as follows:

Literally, it is not difficult to understand the bound parameters. here is an example of binding parameters:

For example:

bind_param("sss", firstname,lastname, $email);

1. this function is bound to the SQL parameter and tells the database parameter value. The "sss" parameter column processes the data types of other parameters. The s character tells the database that this parameter is a string.

There are four types of parameters:

I-integer (integer)
D-double (double floating point type)
S-string (string)
B-BLOB (Boolean)

You must specify a type for each parameter.

By telling the database parameter data type, you can reduce the risk of SQL injection.

2. the firstname, lastname, and $ email mentioned above are references and cannot be directly written as strings after php5.3. to verify this conclusion, I wrote a test section here, as shown below:

$servername="localhost";$username="root";$password="admin";$dbname="test";$conn=new mysqli($servername,$username,$password,$dbname);if($conn->connect_error){ die("connected failed:".$conn->connect_error);}$sql="INSERT INTO user(user_first,user_last,age)VALUES(?,?,?)";$stmt=$conn->prepare($sql);$stmt->bind_param("sss","xiao","hong",22);$stmt->execute();echo "News records created successfully!";$stmt->close();$conn->close();

I wrote a test program that directly writes parameters as strings. after running the program, it will pop up:

Finally, I changed the program to the following:

$servername="localhost";$username="root";$password="password";$dbname="test";$conn=new mysqli($servername,$username,$password,$dbname);if($conn->connect_error){   die("Connect failed:".$conn->connect_error);}$sql="INSERT INTO user(user_first,user_last,age)VALUES(?,?,?)";$stmt=$conn->prepare($sql);$stmt->bind_param("sss",$user_first,$user_last,$age);$user_first="xiao";$user_last="hong";$age=12;$stmt->execute();echo "News records created successfully!";$stmt->close();$conn->close();

The above is the sample code for bind_param () function usage in php. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.