Home > Article > Backend Development > pdo connection database, query and save data to mysql_PHP tutorial
This is an operation example using php+pdo+mysql, which uses pdo to connect to the database, query and save data to mysql.
This is an operation example using php tutorial + pdo + mysql tutorial, which is to use pdo to connect to the database tutorial to query and save data into mysql.
pdo data saving
$pdo = new pdo("mysql:host=localhost;dbname=db_demo","root","");
if($pdo -> exec("insert into db_demo(name,content) values('title','content')")){
echo "Insertion successful!";
echo $pdo -> lastinsertid();
}
?>
Query database
$pdo = new pdo("mysql:host=localhost;dbname=db_demo","root","");
$rs = $pdo -> query("select * from test");
while($row = $rs -> fetch()){
print_r($row);
}
?>