Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!--------------------------------------------PDO PHP DATA OBJECT------------------------->
- ১। PDO এর সুবিধা হল আমরা MYSQL QUERY টা একবার লিখে রেখে তা multiple ভাবে ব্যবহার করতে পারব PDO এর মাধ্যমে।
- ২। কোন প্রকার sql injection যাতে আমার system কে attack করতে না পারে এর জন্য pdo ব্যবহার করা হয় ।
- <!----------------------------------Learn more about PDO see the blew link------------------------------------->
- http://code.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059
- pdo database এর কোন specific syntax নয় কিন্তু এই process টি database এ access করে খুব সহজে।
- <!---------------------------------------connecting database using pdo---------config.php------------------------->
- <?php
- //daatabase connection using pdo
- $dbhost = 'localhost';
- $dbname = 'coderhousebd';
- $dbuser = 'root';
- $dbpass = '';
- try{
- $db = new PDO("mysql:host={$dbhost };dbname={$dbname}",$dbuser,$dbpass);
- $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- }
- catch(PDOException $e){
- echo "Connection error".$e->getMessage();
- }
- //mysql_connect('localhost','root','') or die('can not connect to the server');//old connection process
- //mysql_select_db('coderhousebd') or die('cannot select database');//old connection process
- ?>
- <!----------------------------------------------------------login.php----------------------------------------------->
- ১। প্রথমে mysql query তাকে pdo মাধ্যমে লিখা হচ্ছে মানে prepare() method দিয়ে prepare করা হচ্ছে
- ২। তারপর সেটাকে বিন্ড bind করা হচ্ছে bingParam()method দ্বারা
- ৩। তারপর সেটাকে প্রকাস করা হচ্ছে execute() method দ্বারা
- তবে মনে রাখার বিষয় হল যে database connection দেয়া আছে মানে যে variable দিয়ে database connection দেয়া আছে সেই variable এর মাধ্যমে উপরের কাজ করতে হবে।
- code:
- $statement = $db->prepare("select * from tb_login where username=:username and password=:password");
- $statement->bindParam(":username",$_POST['username']);
- $statement->bindParam(":password",$password );
- $statement->execute();
- এখানে নতুন variable নিয়ে prepare দ্বারা prepare করা হচ্ছে sql query কে
- $db->prepare("select * from tb_login where username=:(this is reference )database field name and password=:(this is reference )database field name");
- $statement->bindParam(":(this is reference )database field name",database field টা কোথায় আছে মানে value কোথায় assign করা আছে);
- $statement->execute();finally execute the value;
- <!---------------------------without bindParam()---------------------another way for PDO shotway------------------------------->
- bindParam() দেয়ার সুবিধা হল যে এটা ব্যবহার করলে চাহিদা অনুযায়ী data show করানো সম্ভব
- আর array() ব্যবহার করার সুবিধা হল code short হয়ে যায় ।
- code :
- //Another way for PDO
- $statement = $db->prepare("select * from tb_login where username=? and password=?");
- $statement->execute(array($_POST['username'],$password));
- $num = $statement->rowCount();
- <!-----------------------------------------------------------------insert.php-------PDO---------------------------------->
- $statement = $db->prepare("insert into tb_student(st_name,st_roll,st_age,st_email) values(?,?,?,?)");
- $statement->execute(array($_POST['st_name'],$_POST['st_roll'],$_POST['st_age'],$_POST['st_email']));
- <!------------------------------------------view.php-----------------------------PDO-------------------------------->
- $statement = $db->prepare("select * from tb_student"); এইটা দ্বারা query prepare() করা হল
- এখানে কোন value অন্য কোন জায়গা থেকে নিয়ে আসার প্রয়োজন নাই তাই সুধু
- $statement->execute(); হবে।
- এখন আমি কিভাবে value গুলো database থেকে নিয়ে দেখাব তার জন্য ঃ
- $result = $statement->fetchAll(PDO::FETCH_ASSOC);আমি একতা variable নিয়ে fetchAll() দ্বারা সব value database থেকে নিয়ে আসলাম , FETCH_ASSOC দ্বারা বুঝাইতেছে যে মান আসবে সেই মানটা হবে associative array .
- code :
- <?php
- //$result = mysql_query("select * from tb_student");
- $i = 0;
- $statement = $db->prepare("select * from tb_student");
- $statement->execute();
- $result = $statement->fetchAll(PDO::FETCH_ASSOC);
- foreach($result as $row){
- $i++;
- ?>
- <tr>
- <td><?php echo $i;?></td>
- <td><?php echo $row['st_name'];?></td>
- <td><?php echo $row['st_roll'];?></td>
- <td><?php echo $row['st_age'];?></td>
- <td><?php echo $row['st_email'];?></td>
- <td>
- <a href="update.php?id=<?php echo $row['st_id'];?>">Edit</a> |
- <a onclick="return confirm_delete();"href="delete.php?id=<?php echo $row['st_id'];?>">Delete</a>
- </td>
- </tr>
- <?php
- }
- ?>
- <!-------------------------------------------------------------------Update.php----------------------------->
- //Data update using PDO
- $statement = $db->prepare("update tb_student set st_name=?,st_roll=?,st_age=?,st_email=? where st_id=?");
- $statement->execute(array($_POST['st_name'],$_POST['st_roll'],$_POST['st_age'],$_POST['st_email'],$id));
- loop
- <?php
- //while($row=mysql_fetch_array($result)){
- //$result = mysql_query("select * from tb_student where st_id='$id' ");
- $statement = $db->prepare("select * from tb_student where st_id=? ");
- $statement->execute(array($id));
- $result = $statement->fetchAll(PDO::FETCH_ASSOC);
- foreach($result as $row){
- $st_name = $row['st_name'];
- $st_roll = $row['st_roll'];
- $st_age = $row['st_age'];
- $st_email = $row['st_email'];
- }
- ?>
- <!--------------------------------------------------------PDO delete.php------------------------------------->
- <?php
- include('config.php');
- if(isset($_REQUEST['id'])){
- $id = $_REQUEST['id'];//catching url request in view.php page
- $statement = $db->prepare("delete from tb_student where st_id=?");
- $statement->execute(array($id));
- //$result = mysql_query("delete from tb_student where st_id='$id'");
- header('location: view.php');
- }
- else{
- header('location: view.php');
- }
- ?>
- <!-----------------------------------------change_password.php--------------------------------------------------->
- $statement = $db->prepare("select * from tb_login where password=?");
- $statement->execute(array($password));
- $num = $statement->rowCount();
- //Data update using PDO
- $statement = $db->prepare("update tb_login set password=? where id=1");
- $statement->execute(array($new_password));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement