In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to use PDO in PHP, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
About 80% of Web applications are supported by PHP. Similarly, the same goes for SQL. Prior to PHP version 5.5, we had mysql_ commands to access the MySQL database, but they were eventually deprecated due to lack of security.
The deprecation happened on PHP 5.5 in 2013. I wrote this article in 2018, and the PHP version is 7.2. The deprecation of mysql_ brings two main ways to access the database: mysqli and PDO libraries.
Although the mysqli library is officially specified, PDO has gained more praise because mysqli can only support mysql databases, while PDO can support 12 different types of database drivers. In addition, PDO has other features that make it a better choice for most developers. You can see some feature comparisons in the following table:
PDOMySQLi database supports 12 drivers, only MySQL sample OOP procedure + OOP preprocessing statement (client side) YesNo1 naming parameter YesNo
Now I think for most developers, the reason why PDO is * is clear. So let's take a closer look at it and hope to cover as much as possible in this article what you need to know about PDO.
Connect
The * step is to connect to the database, and since PDO is fully object-oriented, we will use an instance of the PDO class.
The only thing we need to do is define the host, database name, user name, password, and database character set.
$host= 'localhost';$db =' theitstuff';$user = 'root';$pass =' root';$charset = 'utf8mb4';$dsn = "mysql:host=$host;dbname=$db;charset=$charset"; $conn = new PDO ($dsn, $user, $pass)
Then, as you can see in the code above, we created the DSN variable, which is just a variable that holds the database information. For some people running MySQL on external servers, you can also adjust the port number by providing a port=$port_number.
* you can create an instance of the PDO class. I use the $conn variable and provide $dsn, $user, and $pass parameters. If you follow these steps, you should now have an object called $conn, which is an instance of the PDO connection class. Now is the time to go into the database and run some queries.
A simple SQL query
Now let's run a simple SQL query.
$tis = $conn- > query ('SELECT name, age FROM students'); while ($row = $tis- > fetch ()) {echo $row [' name']. "\ t"; echo $row ['age']; echo "
";}
This is the simplest form of running a query using PDO. We first created a variable called tis (short for TheITStuff), and then you can see that we used the query function in the $conn object we created.
Then we run a while loop and create a $row variable to get the content from the $tis object, and * displays each row by calling the column name.
It's simple, isn't it? Now let's look at the preprocessing statement.
Preprocessing statement
Preprocessing statements are one of the main reasons people started using PDO because it provides statements that block SQL injection.
There are two basic methods available. You can use positional parameters or named parameters.
Position parameter
Let's look at an example of a query that uses location parameters.
$tis = $conn- > prepare ("INSERT INTO STUDENTS (name, age) values"); $tis- > bindValue (1 Magi Mike'); $tis- > bindValue (2 tis- 22); $tis- > execute ()
In the above example, we placed two question marks and then used the bindValue () function to map the values to the query. These values are bound to the position in the question mark of the statement.
I can also use variables instead of providing values directly, as shown in the same example by using the bindParam () function:
$name='Rishabh'; $age=20;$tis = $conn- > prepare ("INSERT INTO STUDENTS (name, age) values (?,)"); $tis- > bindParam (1 dagger name); $tis- > bindParam (2 memage); $tis- > execute (); named parameter
Named parameters are also preprocessing statements that map values / variables to named locations in the query. Since there is no location binding, it is very effective in queries that use the same variable multiple times.
$name='Rishabh'; $age=20;$tis = $conn- > prepare ("INSERT INTO STUDENTS (name, age) values (: name,: age)"); $tis- > bindParam (': name', $name); $tis- > bindParam (': age', $age); $tis- > execute ()
You'll notice that the only change is that I use: name and: age as placeholders and then map variables to them. The colon is used before the parameter, and it is important to let PDO know that the location is a variable.
You can similarly use bindValue () to map values directly using named arguments.
Get data
PDO is very rich in fetching data, and it actually provides many formats to get data from the database.
You can use PDO::FETCH_ASSOC to get the associative array, PDO::FETCH_NUM to get the number array, and PDO::FETCH_OBJ to get the object array.
$tis = $conn- > prepare ("SELECT * FROM STUDENTS"); $tis- > execute (); $result = $tis- > fetchAll (PDO::FETCH_ASSOC)
You can see that I used fetchAll because I want all the matching records. If you only need one line, you can simply use fetch.
Now that we've got the data, it's time to cycle through it, which is very simple.
Foreach ($result as $lnu) {echo $lnu ['name']; echo $lnu [' age']. "
";}
As you can see, because I have requested an associative array, I am accessing each member by name.
Although there is no requirement to define how you want to transfer the delivery data, you can actually set it as the default value when defining the $conn variable itself.
All you need to do is create an array of $options, where you can put all the default configurations, just pass the array in the $conn variable.
$options = [PDO::ATTR_DEFAULT_FETCH_MODE = > PDO::FETCH_ASSOC,]; $conn = new PDO ($dsn, $user, $pass, $options). The above is all the content of the article "how to use PDO in PHP". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.