MYSQL TO PDO

Discussion in 'PHP' started by Peter95, Jan 16, 2017.

  1. Peter95

    Peter95

    Joined:
    Jan 16, 2017
    Messages:
    1
    Likes Received:
    0
    Hi guys, Im new to PDO and I cant get hang of it, could anyone please help me convert mysql code to pdo

    upload_2017-1-16_12-44-29.png
    upload_2017-1-16_12-45-10.png
     
    Peter95, Jan 16, 2017
    #1
    1. Advertisements

  2. Peter95

    Zero Cool

    Joined:
    Jul 23, 2017
    Messages:
    1
    Likes Received:
    0
    First you must to connect your database
    If you want use a function you can use this
    <?php

    define('DBHOST','localhost');
    define('DBUSER','root');
    define('DBPASS','');
    define('DBNAME','example');

    function db_connect
    {
    try{

    $db = new PDO("mysql:host=".DBHOST.";dbname=".DBNAME, DBUSER, DBPASS);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    }
    catch(PDOException $e){
    echo '<div class="alert alert-danger">'.$e->getMessage().'</div>';
    exit;
    }
    return ($db);​
    }
    ?>

    And then to use that's code you can add this

    <?php
    $db = db_connect();
    ?>

    If you want to connect with your class you can add this
    <?php

    define('DBHOST','localhost');
    define('DBUSER','root');
    define('DBPASS','');
    define('DBNAME','example');

    class dbconn {
    public $db;
    public function __construct()
    {

    }
    public function initDBO()
    {
    try {
    $this->db = new PDO("mysql:host=".DBHOST.";dbname=".DBNAME, DBUSER, DBPASS,array(PDO::ATTR_PERSISTENT => true));
    $this->db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    } catch (PDOException $e) {
    echo '<div class="alert alert-danger">'.$e->getMessage().'</div>';
    }
    }
    }
    ?>

    And then if you want use this you can add this on other class
    <?php
    class sql extends dbconn {
    public function __construct()
    {
    $this->initDBO();
    }
    }
    ?>

    If you dont want to use both of them you can just connect like this
    <?php

    define('DBHOST','localhost');
    define('DBUSER','root');
    define('DBPASS','');
    define('DBNAME','example');

    try{

    $db = new PDO("mysql:host=".DBHOST.";dbname=".DBNAME, DBUSER, DBPASS);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    }
    catch(PDOException $e){
    echo '<div class="alert alert-danger">'.$e->getMessage().'</div>';
    exit;
    }
    ?>

    After that now you can use that $db like this
    <?php
    include('connect.php');

    $stmt = ' SELECT * FROM item WHERE type LIKE '.$type.' ';
    $result = $db->query($stmt);

    if($result)
    {
    if($result->rowCount() === 0){
    echo 'Nothing';
    else{
    while($row = $result->fetch(PDO::FETCH_ASSOC)){
    $name = $row['name'];
    $type = $row['type'];
    $price = $row['price'];
    $country = $row['country'];
    $image = $row['image'];
    $review = $row['review'];​
    }​
    }​
    }
    ?>
     
    Zero Cool, Jul 23, 2017
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
  1. Bryan J Gudorf
    Replies:
    0
    Views:
    430
    Bryan J Gudorf
    Sep 23, 2003
  2. Bryan J Gudorf

    Python Database Objects (PDO) 1.0.2 Released

    Bryan J Gudorf, Oct 8, 2003, in forum: Python
    Replies:
    0
    Views:
    433
    Bryan J Gudorf
    Oct 8, 2003
  3. Bryan J Gudorf

    Python Database Objects (PDO) 1.1.0 Released

    Bryan J Gudorf, Nov 3, 2003, in forum: Python
    Replies:
    0
    Views:
    427
    Bryan J Gudorf
    Nov 3, 2003
  4. Bryan J Gudorf
    Replies:
    10
    Views:
    824
  5. Rohini
    Replies:
    0
    Views:
    233
    Rohini
    Feb 23, 2017
  6. SUser
    Replies:
    1
    Views:
    249
    kyrericard
    Apr 24, 2017
  7. Rajvardhan Thakare

    Want to convert python code to java

    Rajvardhan Thakare, Apr 1, 2017, in forum: Python
    Replies:
    0
    Views:
    224
    Rajvardhan Thakare
    Apr 1, 2017
  8. AtomicBunny

    Im looking to learn how to code.

    AtomicBunny, Jul 1, 2017, in forum: C Programming
    Replies:
    2
    Views:
    93
    Thiefcom
    Jul 2, 2017
Loading...