Detailed explanation of how to use php PDO_PHP tutorial
This article will give you a detailed introduction to how to use php PDO. You are welcome to refer to it. Friends who need to know more can save this article.
PDO::exec
The returned type is int, indicating the number of items that affect the result.
PDOStatement::execute
The returned value is boolean, true indicates successful execution, false indicates execution failure.
These two usually appear in:
The code is as follows | Copy code | ||||
$pre = $pdo->prepare($sql); |
Generally, you can use the value of $rs0 to determine whether the SQL execution is successful or not. If the value is false, it means that the SQL execution failed, 0 means no changes, and a value greater than 0 means how many records were affected.
However, $rs1 can only return whether the SQL execution is successful or not. If you need to get the number of affected records, you need to use $pre->rowCount();
I personally like to use MySQL, so I have these two lines in my extensions.ini
extension=pdo.so
extension=pdo_mysql.so
代码如下 | 复制代码 |
define('DB_NAME','test'); |
Code: [Select]
The code is as follows | Copy code |
define('DB_NAME','test'); define('DB_USER','test');define('DB_PASSWD','test'); define('DB_HOST','localhost');define('DB_TYPE','mysql'); $dbh = new PDO(DB_TYPE.':host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASSWD);
|
When operating like the above, $dbh itself represents the PDO connection
代码如下 | 复制代码 |
$sql = 'select * from test'; |
So what? Use PDO?
Don’t think about anything, just use the query function as usual
Code: [Select]
The code is as follows | Copy code | ||||
$sql = 'select * from test'; foreach ( $dbh->query($sql) as $value){ echo $value[col];
|
The code is as follows | Copy code |
$sth = $dbh->prepare('update db set zh_CN= :str where SN=:SN'); $sth->bindParam(':str',$str,PDO::PARAM_STR,12); $sth->bindParam(':SN',$SN); $sth->execute(); |
Please pay attention to :str and :SN in the article. When we use the bindParam function, we can use :word to specify the part that the system needs to apply. For example, we use :str and :SN to specify
As for the actual content, bindParam can also specify the type we want to input.
首先我们先看:str 的指定,:str 由於我确定资料是属於文字,因此利用PD::PARAM_STR 来告诉程式“这个是字串哟”,并且给一个范围,也就是长度是12个Bit.
We can also avoid that complexity, like :SN. Although it is also specified using bindParam, we omit the type and length. PHP will use the default type of the variable.
Finally, use $sth->execute(); to perform the execution action.
Basically it’s not difficult, it can even be said to be very simple!
If you have a large amount of data that needs to be applied repeatedly, you can desperately reuse bindParam to specify, such as my :str and :SN. If there are ten pieces of data, I can also add it directly to the data like this Library
Code: [Select]
The code is as follows | Copy code | ||||
foreach ($array => $value ) $sth->bindParam(':SN',$value[SN]); $sth->execute();
} |
代码如下 | 复制代码 |
$sth = $dbh->prepare('select * from db where SN = :SN'); |
Then, if you use prepare to select, of course the keywords can also be specified using :word as above
Code: [Select]
The code is as follows | Copy code |
$sth = $dbh->prepare('select * from db where SN = :SN');
$sth->bindParam(':SN',$value[SN]);
echo $meta["name"]; |
This? The new one is fetch, which has the same meaning as mysql_fetch_row()
But in fetch() we found an extra PDO::FETCH_ASSOC
fetch() provides many ways to obtain data, and PDO::FETCH_ASSOC refers to returning the field name and value of the next data For example, in the above example, use $meta to obtain the data returned by fetch. At this time
The element name of $meta is the field name of the database, and the content is of course the value itself
代码如下 | 复制代码 |
$sth = $dbh->prepare('select * from db where SN = :SN'); |
The code is as follows | Copy code |
$sth = $dbh->prepare('select * from db where SN = :SN'); $sth->bindParam(':SN',$value[SN]); $sth->execute(); if ($sth->errorCode()) { echo "There is an error! There is an error!"; print_r($sth->errorInfo()); } |
And $sth->errorInfo() will be an array, this array has three values
0 is SQLSTATE error code
1 The error code returned by the Driver you are using
2 Error message returned by the Driver you are using

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
