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 > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article shows you how to use pdo to connect to the database in php, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
To connect to the database using php, you must first instantiate the class of pdo, and have the data source, server account, and server password.
A data source is a collection of database types, server names, and database names.
Query ("set names gbk"); / / sets the encoding format of the data passed from the database
? >
Transaction introduction: I will explain the transaction introduction through my own understanding, that is, first turn off the automatic commit function of the database (what is the automatic commit function? That is, when we finish writing a sql statement, we can't afford to press enter, but we have to go through special code processing before submitting it, which I will introduce later.)
Then write the sql statement you want to execute and assign the returned results to two different variables, and then commit. If one or more of the errors occur during execution, the transaction rollback will be carried out. Even if you return to the initial state (that is, all the statements inserted or changed or deleted or queried in the transaction code are invalidated), another advantage is that you will not enter other web pages. Or execute other sql statements that affect the transaction
/ / the following is a brief introduction to the code for transaction rollback
Query ("set names gbk"); / / sets the encoding format of the data passed from the database
$pdo- > begintransaction (); / / disable the autocommit function of mysql here
Query ("insert into tongxue values ('130042106,' Tan Yong', 'male')
Query ("insert into tongxue values ('130042100)' Zhu Bajie', 'male'")
If ($a==true & & $b==true) {
$pdo- > commit (); / / commit transaction
} else {
$pdo- > rollback (); / / transaction rollback
}
? >
The mysql_num_rows () function can count the rows of the result set returned by the database to determine whether the user name and password entered by the user are correct, so how do we achieve this function in pdo?
In pdo, there is a function pdo::fetchall (). Its function is to assign all the result sets returned from the database to get its value, and then use the count () function to count the number of trips. The specific case code is as follows
Query ("set names gbk"); / / sets the encoding format of the data passed from the database
$sql= "select * from tongxue where id='130042106'"
$shuju=pdo- > prepare ($sql); / / this is what we call preprocessing
$shuju- > execute (); / / result of performing preprocessing
$jg=$shuju- > fetchall (PDO::FETCH_ASSOC); / / return all the returned result sets to the variable $jg as an array
$hangshu=count ($jg); / / count the rows of the result set
If ($hangshu > 0) {
Echo "found out that there is this person."
} else {
Echo 'query shows that there is no such person'.
}
? >
When our page runs a lot of sql statements, we can use the preprocessing in pdo to ease the pressure on the server, which is a good choice for projects that want to do large websites, because large websites get tens of thousands or hundreds of thousands of views a day.
Let's take a look at the preprocessing of pdo.
Query ("set names gbk"); / / sets the encoding format of the data passed from the database
$sql= "insert into tongxue values ('130042100' teacher', 'male')"; / / the sql statement we are going to execute
$shuju=$pdo- > prepare ($sql); / / preprocessing
$shuju- > execute (); / / execute preprocessed sql statements
If ($shuju) {
Echo 'execution successful'
} else {
Echo 'execution failed'
}
? >
The above is how to use pdo to connect to the database in php. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are 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.