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

What are the methods of php pdo class

藏色散人
藏色散人Original
2020-10-22 09:34:162079browse

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}的数据插入成功!";
}
?>

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;
'; ?>

***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;
'; ?>

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;
'; ?>

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!

Statement:
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