Jump to content

pdo question

- - - - -

  • Please log in to reply
3 replies to this topic

#1
promehdioh

promehdioh

    Newbie

  • Members
  • PipPip
  • 12 posts

hello , I have a question in php data objects.
how to write pdo functions in my own class?
I would using pdo in my own classes but I don't have any way for that


Edited by promehdioh, 20 February 2011 - 10:11 AM.


#2
grisha

grisha

    Learning Programmer

  • Members
  • PipPipPip
  • 35 posts
Can you be more specific?

A good thing to start is

<?php

$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);

?>

Check the manual for more info

#3
promehdioh

promehdioh

    Newbie

  • Members
  • PipPip
  • 12 posts
thank you my friend for your answer
I create a class ,in this class I would use pdo functions (from pdo class or pdostatement class) in class not over this

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
You can extend the PDO/statement class any manner you like, i.e.

class MyDB extends PDO {
  $m_lastquery = '';
  public function __construct ($dsn, $username, $password, $driver_options=null){
    parent::__construct($dsn, $username, $password, $driver_options);
  }
  public function doSomethingWithPdo($query) {
    $this->m_lastquery = parent::prepare($query);
    $this->m_lastquery->execute();
  }
}

// example use
$query = "INSERT INTO table VALUES (1, 2, 3)";

$mydb = new MyDB("localhost", "root", "password");
$mydb->doSomethingWithPdo($query);
I would brush up on object oriented programming with PHP to get the most out of working with the classes.

Edited by Alexander, 23 February 2011 - 06:52 PM.

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users