Monday 10 November 2014

Basic PDO function in php

Mysql deprecated after every one use mysqli but its fully oops based concept so beginners something they feel difficult to understand the basic query in mysqli i strongly suggested to this post that kind of persons. here we already created some PDO functions for more flexible purpose. follow below code you should learn PDO basic function.

php data object

DOWNLOAD                LIVE DEMO

DATABASE
 Create sample database table user_data
CREATE TABLE IF NOT EXISTS `user_data` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` text NOT NULL,
`description` text NOT NULL
PRIMARY KEY (`id`)
)

BASIC.PHP
<?php
function __autoload($name) {
    require $name . '.php';
}
$DBVARS=array(
    'username'=>'root',
    'password'=>'',
    'hostname'=>'localhost',
    'db_name'=>'1next2'
);
function dbAll($query,$key='') {
    $q = dbQuery($query);
    $results=array();
    while($r=$q->fetch(PDO::FETCH_ASSOC))$results[]=$r;
    if(!$key)return $results;
    $arr=array();
    foreach($results as $r)$arr[$r[$key]]=$r;
    return $arr;
}
function dbInit(){
    if(isset($GLOBALS['db']))return $GLOBALS['db'];
    global $DBVARS;
    $db=new PDO('mysql:host='.$DBVARS['hostname'].';dbname='.$DBVARS['db_name'],$DBVARS['username'],$DBVARS['password']);
    $db->query('SET NAMES utf8');
    $db->num_queries=0;
    $GLOBALS['db']=$db;
    return $db;
}
function dbOne($query, $field='') {
    $r = dbRow($query);
    return $r[$field];
}
function dbLastInsertId() {
    return dbOne('select last_insert_id() as id','id');
}
function dbQuery($query){
    $db=dbInit();
    $q=$db->query($query);
    $db->num_queries++;
    return $q;
}
function dbRow($query) {
    $q = dbQuery($query);
    return $q->fetch(PDO::FETCH_ASSOC);
}

INDEX.PHP
dbAll($query) used to show all table record values , dbRow($query) this query used to show particular data or single data value.
<html>
    <head>
    </head>
        <?php
            include_once('basic.php');
            $topics="SELECT * FROM user_data";
            $topic_list=dbAll($topics);  
            $one_list=dbRow($topics);  
        ?>
    <body>
        <div> 
                <h2>Show All topic</h2>
            <ul>
        <?php
            foreach ($topic_list as $key => $list) { 
        ?>  
                <li><?php echo $list['title'] ?> </li>
                   <?php echo $list['description'] ?>
        <?php } ?>      
            </ul>
        </div>

        <div> 
                <h2>one topic</h2>
            <ul>
                <li><?php echo $one_list['title'] ?> </li>
                    <?php echo $one_list['description'] ?>      
            </ul>
        </div>
    </body>
</html>

  I hope this topic helpful for you. if you want more update subscribe your email @ www.1next2.com   

9 comments:

  1. Undoubtedly, this article turned out to be considerable for me and I am sure this might be advantageous for many seekers who are beginning with PHP or even for the experienced one. I admire you for this great work. Keep blogging.
    Website Designer in Lucknow | Web design company

    ReplyDelete

© All rights reserved @ 1next2.com