How to Connect to the MySQL Database in PHP – Advanced Tutorial

Share with your friends!

කලින් වතාවේ සරලව MySQL database එකක් PHP වලින් use කරන විදිය ගැන කතා කරානේ.. ඒ පොස්ට් එක මෙතනින් බලන්න පුළුවන්. දැන් මේ කියන්න යන්නේ ඊට වැඩිය advanced method එකක්. මේකෙදි PDO – Php Data Object කියන PHP Extension එක use කරලා තමයි වැඩේ කරන්නේ. සරලව පැහැදිලි කරනවා නම් මේ PDO එකෙන් කරන්නේ MySQL Connection එක manage කරන එක.

කලින් පැහැදිලි කරපු විදියට වැඩිය මේ විදියෙන් DB connection එක තවදුරටත් stable කරනවා well managed structure එකකින්. ඒ වගේම දැනට භාවිතා වෙන සියලුම Database platforms එක්ක වැඩ කරන්න පුළුවන් වෙන එකත් මේ PDO ජනප්‍රිය වෙන්න තවත් හේතුවක්. MySQL, Oracle, PostgreSQL, FirebirdSQLITE වගේ db platform ගණනාවකට support කරනවා. php server eke මේ script එක run කරලා බලන්න Database එකට PDO drivers තියනවද කියලා.

[code language=”php”]
<?php
foreach(PDO::getAvailableDrivers() as $driver)
{
echo $driver.'<br/>’;
}
?>;
[/code]

මේකෙදි getAvailableDrivers() කියන static method එක use කරන්නේ. මේකෙන් තියන drivers එයි. default MySQL, SQLITE එනවා xampp වලට. එහෙම නැත්තන් configure කරගන්න වෙනවා.

මීළඟට තියෙන්නේ connection එක හදාගන්න. transaction වලට කලින් database connection එක හදාගන්න ඕන. database එකේ type එක වෙනස් උනත් මේ step එක වෙනසක් වෙන්නේ නෑ.

[code language=”php”]
<?php

$hostname = ‘localhost’;
$username = ‘username’;
$password = ‘password’;

try {
$dbhandler = new PDO("mysql:host=$hostname;dbname=mysql", $username, $password);
/*** response from DB ***/
echo ‘Connected to database’;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>;
[/code]

username, password වැරදි නැත්තන් මෙහෙම output එකක් එනවා.

[code language=”php”]
Connected to database
[/code]

දැන් connection එක establish කරගන්න part එක හරි. දැන් බලන්න ඕන CRUD operation කරන්නේ කොහොමද කියලා.

INSERT

use කරන database එකයි tabel එකයි ඕන කරනවා. ඒ වගේම columns වල datatypes ඕන කරනවා. ‘USE dbName’, ‘DESCRIBE tableName’ කියන commands වලින් table structure එක බලන්න පුළුවන් MySQL Command Line එකෙන්, Phpmyadmin වගේ එකක් use කරනවා නම් table එක select කරලා බලන්න පුළුවන්.

mcl

මම use කරලා තියෙන්නේ test කියන database එකයි foods කියන table එකයි.

[code language=”php”]
<?php
$hostname = ‘localhost’;
$username = ‘username’;
$password = ‘password’;

try {
$dbhandler = new PDO("mysql:host=$hostname;dbname=test", $username, $password);
/*** response from DB ***/
echo ‘Connected to database<br>’;

/*** INSERT query ***/
$count = $dbhandler->exec("INSERT INTO foods(name, desc) VALUES (‘Kandos’, ‘Chocolate’)");

/*** Number of affected rows ***/
echo $count;

/*** close the db connection ***/
$dbhandler = null;
}
catch(PDOException $e)
{
/*** get message if exception throws ***/
echo $e->getMessage();
}
?>;
[/code]

$count කියන variable එකෙන් query එකෙන් affect වුන rows count එක එනවා. මේකෙදි එකක් උනාට update එකකදී එහෙම rows කීපයක් වෙන්න පුළුවන්.

මේ වගේම prepared statement use කරන්නත් පුළුවන්.

[code language=”php”]
<?php
$hostname = ‘localhost’;
$username = ‘username’;
$password = ‘password’;

try {
$dbhandler = new PDO(&quot;mysql:host=$hostname;dbname=test&quot;, $username, $password);
/*** response from DB ***/
echo ‘Connected to database<br/>’;

/*** INSERT query ***/
$query = "INSERT INTO foods VALUES (?, ?, ?)";
$qry = $dbhandler->prepare($query);
$count = $qry->execute(array(1,’Kandos’,’Chocolate’))

/*** Number of affected rows ***/
echo $count;

/*** close the db connection ***/
$dbhandler = null;
}
catch(PDOException $e)
{
/*** get message if exception throws ***/
echo $e->getMessage();
}
?>;
[/code]

Update, Delete queries execute වෙන්නෙත් මේ විදියටමයි. ඒක නිසා ඒ ගැන විස්තර කරන්න යන්නේ නැහැ. query එක විතරයි වෙනස් වෙන්නේ. දැන් data retrieve කරගන්න විදිය බලමු.

SELECT

database table rows එන්නේ array විදියට. ඒ array වලින් data අරගන්න එක තමයි මේකෙදි කරන්නේ.

[code language=”php”]
<?php
$hostname = ‘localhost’;
$username = ‘username’;
$password = ‘password’;

try {
$dbhandler = new PDO("mysql:host=$hostname;dbname=test", $username, $password);
/*** response from DB ***/
echo ‘Connected to database<br/>’;

/*** SELECT QUERY ***/
$sql = "SELECT * FROM foods";
foreach ($dbhandler-&gt;query($sql) as $row)
{
print $row[‘name’] .’ and ‘. $row[‘desc’] . ‘<br/>’;
}

/*** close the db connection ***/
$dbhandler = null;
}
catch(PDOException $e)
{
/*** get message if exception throws ***/
echo $e->getMessage();
}
?>;
[/code]

මේකෙන් use කරන table එකේ data retrieve කරගන්න පුළුවන්. print $row[‘name’] .’ – ‘. $row[‘desc’] . ‘
‘  
වෙනුවට තමන් කැමති විදියට data manage කරගන්න පුළුවන්. use කරන table එකේ තියන table  columns අනුව variables වල නම් වෙනස් කරගන්න.

FETCH DATA AS CLASS

මේ කියන්න හදන්නේ data fetch කරද්දී එහෙමත් නැත්තන් retrieve කරද්දී class objects විදියට ගන්නේ කොහොමද කියලා.

[code language=”php”]
<?php

class foods{

public $id;
public $name;
public $desc;

public function getData(){
echo $this->id.’,’.$this->name.’,’.$this->desc;
}
}

$hostname = ‘localhost’;
$username = ‘username’;
$password = ‘password’;

try {
$dbhandler = new PDO("mysql:host=$hostname;dbname=test", $username, $password);
/*** response from DB ***/
echo ‘Connected to database<br/>’;

/*** SELECT QUERY ***/
$sql = "SELECT * FROM foods";

/*** fetch into an PDOStatement object ***/
$stmt = $dbhandler->query($sql);

/*** fetch into the foods class ***/
$obj = $stmt->fetchALL(PDO::FETCH_CLASS, ‘foods’);

/*** loop of the object directly ***/
foreach($obj as $food)
{
/*** call the getData method ***/
echo $food->getData().'<br/>’;
}
/*** close the db connection ***/
$dbhandler = null;
}
catch(PDOException $e)
{
/*** get message if exception throws ***/
echo $e->getMessage();
}
?>;
[/code]

use කරන table එකේ තියන table  columns අනුව class එකේ variables වල නම් වෙනස් කරගන්න.

Share with your friends!

1 comment

Leave A Comment

shares