Home > Backend Development > PHP Problem > What are the methods of php pdo class

What are the methods of php pdo class

藏色散人
Release: 2023-03-06 19:08:02
Original
2235 people have browsed it

php pdo class methods include: 1. exec method; 2. query method; 3. lastInsertId method; 4. setAttribute method, etc.

What are the methods of php pdo class

# Recommended: "

PHP Video Tutorial

Common methods of pdo class:

exec()

query()

lastInsertId()

<?php
$servername = "localhost";
$username = "root";
$password = "133nubia022";
$dbname=&#39;myweb&#39;;

$pdo= new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$pdo->exec(&#39;set names utf8&#39;);

$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_BOTH);

$sql="insert into user(username,age) values(&#39;user123&#39;,&#39;55&#39;)";

if($pdo->exec($sql)){
	$lastid=$pdo->lastInsertId();
	echo "ID为{$lastid}的数据插入成功!";
}
?>
Copy after login

What are the methods of php pdo class

• setAttribute()


Set the get mode attribute

<?php
$servername = "localhost";
$username = "root";
$password = "133nubia022";
$dbname=&#39;myweb&#39;;
 

$pdo= new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);

$pdo->exec(&#39;set names utf8&#39;);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);

$sql="select * from user";

$smt=$pdo->query($sql);
$rows=$smt->fetchAll();

echo &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r($rows);
echo &#39;
Copy after login
'; ?>

***What are the methods of php pdo class
Get the index array

<?php
$servername = "localhost";
$username = "root";
$password = "133nubia022";
$dbname=&#39;myweb&#39;;

$pdo= new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$pdo->exec(&#39;set names utf8&#39;);

$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_NUM);

$sql="select * from user";
$smt=$pdo->query($sql);
$rows=$smt->fetchAll();

echo &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r($rows);
echo &#39;
Copy after login
'; ?>

What are the methods of php pdo class


##Get the mixed array

<?php
$servername = "localhost";
$username = "root";
$password = "133nubia022";
$dbname=&#39;myweb&#39;;

$pdo= new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$pdo->exec(&#39;set names utf8&#39;);

$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_BOTH);

$sql="select * from user";
$smt=$pdo->query($sql);
$rows=$smt->fetchAll();

echo &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r($rows);
echo &#39;
Copy after login
'; ?>

What are the methods of php pdo class

The above is the detailed content of What are the methods of php pdo class. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template