0% found this document useful (0 votes)
67 views

Mysqli Et PDO

This document compares Mysqli and PDO for connecting to and interacting with MySQL databases in PHP. It provides code examples for connecting to a database and performing common operations like insertion, listing, modification, deletion of data. For Mysqli, it shows how to connect to a database, insert data, fetch data in different formats. For PDO, it demonstrates connecting to a database and performing insertion, listing, deletion and modification of data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Mysqli Et PDO

This document compares Mysqli and PDO for connecting to and interacting with MySQL databases in PHP. It provides code examples for connecting to a database and performing common operations like insertion, listing, modification, deletion of data. For Mysqli, it shows how to connect to a database, insert data, fetch data in different formats. For PDO, it demonstrates connecting to a database and performing insertion, listing, deletion and modification of data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Mysqli et PDO

Mysqli – Connexion à la base de données


<?php
define('DB_SERVER','localhost');
define('DB_USER','root');
define('DB_PASS' ,'');
define('DB_NAME', 'dbname');
$con = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
// Check connection
if (mysqli_connect_errno())
{
 echo “Erreur de connexion: " . mysqli_connect_error();
}
?>
<?php
$con=mysqli_connect("localhost", "root", "", "dbname");
// Check connection
if (mysqli_connect_errno())
{
 echo “Erreur de connexion: " . mysqli_connect_error();
}
?>
Mysqli - Insertion

--Table data
CREATE TABLE IF NOT EXISTS `data` (
`id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`contactno` bigint(20) NOT NULL,
`gender` varchar(255) NOT NULL,
`education` varchar(255) NOT NULL,
`address` longtext NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
config.php mysqli_insert.php
Mysqli- Liste

mysqli_fetch_array.php mysqli_fetch_row.php mysql_fetch_object.php


config.php
Mysqli- modification et suppression

index.php edit.php
PDO
CREATE TABLE `users` ( `id` int(11) NOT NULL,
`name` varchar(60) DEFAULT NULL,
`phone` varchar(12) DEFAULT NULL,
`city` varchar(60) DEFAULT NULL,
`date_added` date DEFAULT NULL)
ENGINE=InnoDB DEFAULT CHARSET=latin1;
PDO- Connexion
// DB credentials.
define('DB_HOST','localhost');
define('DB_USER','your user name');
define('DB_PASS','your user password');
define('DB_NAME','your database name');
// Establish database connection.
try
{
$dbh = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME,DB_USER, DB_PASS,
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
}
catch (PDOException $e)
{
exit("Error: " . $e->getMessage());
}

//Fermé la connection
$dbh = null;
PDO-insertion et liste

PDOinsertion_liste.php
PDOconfig.php
PDO-suppression
$sql = "DELETE FROM `users` WHERE `id`=:id";
$query = $dbh -> prepare($sql);
$query -> bindParam(':id', $id, PDO::PARAM_INT);
$id = 1;
$query -> execute();
if($query -> rowCount() > 0)
{
$count = $query -> rowCount();
echo $count . " rows were affected.";
}
else
{
echo "No affected rows.";
}
PDO -Modification
$sql = "UPDATE users
SET `city`= :city, `phone` = :tel
WHERE `id` = :id";
$query = $dbh->prepare($sql);
$query -> bindParam(':city', $city, PDO::PARAM_STR);
$query -> bindParam(':tel' , $tel , PDO::PARAM_INT);
$query -> bindParam(':id' , $id , PDO::PARAM_INT);
$tel = '02012345678';
$city = 'London';
$id = 1;
$query -> execute();
if($query -> rowCount() > 0)
{
$count = $query -> rowCount();
echo $count . " Ligne bien modifier .";
}
else
{
echo “Erruer...";
}

You might also like