Jump to content

cybernet

Members
  • Posts

    88
  • Joined

  • Last visited

Posts posted by cybernet

  1. so i found a pdo class to connect to mysql with pdo and looks easy to customize

     

    however i have an error that i can't figure it out

     

    class

    <?php
    
    class ApplicationResourcePool 
    {
        static var $_dbHandle;   // line 5
    
        private static $_dbConfig = array(
            'dsn'      => 'mysql:dbname=vax',
            'username' => 'vax',
            'password' => 'veryrandpasswd',
    
        );
        public static getDbHandle(){
            if(self::$_dbHandle == null){
                self::$_dbHandle = new PDO(
                    self::$_dbConfig['dsn'] , 
                    self::$_dbConfig['username'], 
                    self::$_dbConfig['password'] 
                );
            }
            return self::$_dbHandle;
        }
    
    }
    
    class StockMapper
    {
        protected $_dbh; 
        public __construct($dbh = null)
        {
            if($dbh == null){
                $this->_dbh = ApplicationResourcePool::getDbHandle();
            } else {
                $this->_dbh = $dbh;
            }
    
        }
        public getStockById($stockId){
    
            $sth=$this->_dbh->prepare("SELECT * from stock WHERE id = :stockId"); 
            $sth->bindParam(":stockId", $stockId);
            $sth->execute();
            $result = $sth->fetch(PDO::FETCH_ASSOC);
            return $result[0];
        }
    }
    
    $stockMapper = new StockMapper();
    $stockData = $stockMapper->getStockById('302');
    
    ?>
    
    Parse error: syntax error, unexpected 'var' (T_VAR), expecting variable (T_VARIABLE) in /path/index.php on line 5
    

    i really don't know what to do :sweat:

  2. How about PDO itself? Why would you want to wrap PDO inside a class? From your example above I don't see you add anything useful.

     

    i'm building a project for the first time with PDO

     

    this is how i configured it

     

    index.php

     

    <?php
    require_once (dirname(__FILE__) . '/inc/main.php');
    
    userlogin();
    
    $HTMLOUT = '';
    
    $stmt = $db->query("SELECT row_id, name, mobile FROM location LIMIT 3");
    // $stmt->execute(array($id, $name));
    $stmt->setFetchMode(PDO::FETCH_OBJ);
    
    
    $db = null;
    $stmt = null;
    
    ?>

     

    inc/main.php

     

    <?php
    
    require_once (dirname(__FILE__) . '/pdo_conn.php');
    require_once (dirname(__FILE__) . '/mail/class.Mail.php');
    
    error_reporting(E_ALL);
    
    
    function userlogin() {
    global $db;
    unset($GLOBALS["CURUSER"]);
    
    $ip = getip();
    $nip = ip2long($ip);
    
    $id = 0 + get_cookie('uid');
    
    
    $fetch_user_details = $db->prepare("SELECT * FROM users WHERE user_id = :bitches_id LIMIT 1");
    $fetch_user_details->bindParam(':bitches_id', $id, PDO::PARAM_INT);
    $fetch_user_details->execute();
    $fetch_user_details->setFetchMode(PDO::FETCH_OBJ);
    $row = $fetch_user_details->fetch();
    
    
    $user_id = $row->user_id;
    $user_ip = $row->user_last_ip;
    $update_user_details = $db->prepare("UPDATE users SET user_last_access = UNIX_TIMESTAMP(), user_last_ip = :last_ip WHERE user_id = :u_id LIMIT 1");
    $update_user_details->bindParam(':last_ip', $user_ip, PDO::PARAM_STR, 15);
    $update_user_details->bindParam(':u_id', $user_id, PDO::PARAM_INT);
    $update_user_details->execute();
    
    $GLOBALS["CURUSER"] = $row;
    
    }
    
    function is_logged_in() {
    global $CURUSER;
    if (!$CURUSER) {
    header("Location: domain.net/login.php?403");
    exit();
    }
    }
    
    ?>

     

    inc/pdo_conn.php

     

    <?php
    
    $db = new PDO('mysql:host=localhost;dbname=abc;charset=UTF-8', 'abc', 'xam');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    
    ?>

     

    until a few days ago it worked, but when i starting to expand my project

    i started to get this error

     

    PHP Fatal error: Call to a member function prepare() on a non-object in /root/inc/main.php"
    

     

    the error reffers to this line

    $fetch_user_details = $db->prepare("SELECT * FROM users WHERE user_id = :bitches_id LIMIT 1");
    

  3. can anyone of you can give me a database class that uses PDO ?

     

    something like this

    class SQL {
    private static $dbh = null;
    private function __construct($host, $user, $pass, $data, $table) {
    $this->host = (!empty($host)) ? $host: 'localhost';
    $this->user = $user;
    $this->pass = $pass;
    $this->data = $data;
    $this->table = $table;
    static::connect();
    }
    private function __destruct() {
    static::close();
    }
    public static function connect() {
    $dsn = 'mysql:dbname=' . $this->table . ';host=' . $this->host;
    static::$dbh = new PDO($dsn, $this->user, $this->pass);
    static::$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
    public static function close() {
    static::$dbh = null;
    }
    public static function query($query) {
    $sth = static::$dbh->prepare($query);
    $sth->execute();
    }
    }
    

     

    but should include query count, if possible ^_^

  4. Glad I could help, though I must confess I'm a bit confused as to exactly how I did that. :P

    In any case, you're welcome.

     

    (Thought you were asking for a class that could use PDO, when PDO is a class itself.)

     

    1. Send the data as a parameter instead, like you've done with the $name.

    2. yes, i was asking for a class that could use PDO

     

    like this one

     

    class SQL {
    private static $dbh = null;
    private function __construct($host, $user, $pass, $data, $table) {
    $this->host = (!empty($host)) ? $host: 'localhost';
    $this->user = $user;
    $this->pass = $pass;
    $this->data = $data;
    $this->table = $table;
    static::connect();
    }
    private function __destruct() {
    static::close();
    }
    public static function connect() {
    $dsn = 'mysql:dbname=' . $this->table . ';host=' . $this->host;
    static::$dbh = new PDO($dsn, $this->user, $this->pass);
    static::$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
    public static function close() {
    static::$dbh = null;
    }
    public static function query($query) {
    $sth = static::$dbh->prepare($query);
    $sth->execute();
    }
    }
    

     

    so now i should use :

     

    function userlogin($db) {
    unset($GLOBALS["CURUSER"]);
    } // etc
    

    instead of :

    function userlogin() {
    global $db;
    unset($GLOBALS["CURUSER"]);
    } // etc

     

    ?

  5. i said that pdo is a class, because at the time i wrote the post, i remembered a code line that began with class XyZ extends DB { ...

     

    where DB was the pdo connect thing ...

     

    @Christian F, thank you for the information

    you really clarified that error for me ^_^

  6. function get_cookie($name)
       {
      global $ResT_BV;
    
     if ( isset($_COOKIE[$ResT_BV['cookie_prefix'].$name]) AND !empty($_COOKIE[$ResT_BV['cookie_prefix'].$name]) )
     {
      return urldecode($_COOKIE[$ResT_BV['cookie_prefix'].$name]);
     }
     else
     {
      return FALSE;
     }
    }
    

     

    function getip() {
      if (isset($_SERVER)) {
     if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && validip($_SERVER['HTTP_X_FORWARDED_FOR'])) {
       $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
     } elseif (isset($_SERVER['HTTP_CLIENT_IP']) && validip($_SERVER['HTTP_CLIENT_IP'])) {
       $ip = $_SERVER['HTTP_CLIENT_IP'];
     } else {
       $ip = $_SERVER['REMOTE_ADDR'];
     }
      } else {
     if (getenv('HTTP_X_FORWARDED_FOR') && validip(getenv('HTTP_X_FORWARDED_FOR'))) {
       $ip = getenv('HTTP_X_FORWARDED_FOR');
     } elseif (getenv('HTTP_CLIENT_IP') && validip(getenv('HTTP_CLIENT_IP'))) {
       $ip = getenv('HTTP_CLIENT_IP');
     } else {
       $ip = getenv('REMOTE_ADDR');
     }
      }
      return $ip;
    }
    

     

    i didn't include this in the 1st post because they were irrelevant to the issue i'm having

     

    so does anyone have a simple class { database } to use with pdo ?

     

    please :unsure:

  7. well, as you can see in my signature i'm a MySQL freak :tease-03:

    so i would like to create a class that extends pdo and connects to mysqli

     

    i want to use pdo because it's safe for queries

     

    for now all i want is a class that connects to MySQL with mysqli ( it's supposed to be iMproved ), one that has a query count, and possible a way to cache queries

     

    for start if you could help only with the class, and the rest i will try to figure out myself :D

     

    thanks in advance :happy-03:

  8. if i would do that, then i have to call userlogin() on each page.

     

    on a search on google i found that i should create a class to connect to database, but i really don't know how to make it secure and flawless

     

    http://stackoverflow...object-php-help

     

    i found some tuts online, but there are many stuff that i don't need ( at least for know )

     

    this would be an example

     

    http://www.tiny-thre...o-class-update/

     

    i know that i sound very ... n00b and dumb also, but i really need an advice here :shrug:

     

    @muddy i've checked youre blog and i saw that you have something there with classes

     

    http://muddy-dev.blogspot.ro/2012/10/php-classes-simple-sample-class.html

     

    but i didn't find a class that extends pdo ( mysql )

     

    can you give me a tip, tutorial, something ?

  9.  

    What debugging have you done already?

     

    To be fair though, none of these are the biggest problem with your code.

     

    1. none :-\

    2. like i said this is the first time i'm using pdo ... so i did how i thought at the moment

    Don't try and force functions to use variables declared in the globals namespace, it defeats the a big part of there purpose.

     

    how should i proceed ? can you please help me :confused:

     

    thanks in advance :happy-04:

  10. i'm building a project for the first time with PDO

     

    this is how i configured it

     

    index.php

     

    <?php
    require_once (dirname(__FILE__) . '/inc/main.php');
    
    userlogin();
    
    $HTMLOUT = '';
    
    $stmt = $db->query("SELECT row_id, name, mobile FROM location LIMIT 3");
    // $stmt->execute(array($id, $name));
    $stmt->setFetchMode(PDO::FETCH_OBJ);
    
    
    $db = null;
    $stmt = null;
    
    ?>

     

    inc/main.php

     

    <?php
    
    require_once (dirname(__FILE__) . '/pdo_conn.php');
    require_once (dirname(__FILE__) . '/mail/class.Mail.php');
    
    error_reporting(E_ALL);
    
    
    function userlogin() {
    global $db;
    unset($GLOBALS["CURUSER"]);
    
    $ip = getip();
    $nip = ip2long($ip);
    
    $id = 0 + get_cookie('uid');
    
    
    $fetch_user_details = $db->prepare("SELECT * FROM users WHERE user_id = :bitches_id LIMIT 1");
    $fetch_user_details->bindParam(':bitches_id', $id, PDO::PARAM_INT);
    $fetch_user_details->execute();
    $fetch_user_details->setFetchMode(PDO::FETCH_OBJ);
    $row = $fetch_user_details->fetch();
    
    
    $user_id = $row->user_id;
    $user_ip = $row->user_last_ip;
    $update_user_details = $db->prepare("UPDATE users SET user_last_access = UNIX_TIMESTAMP(), user_last_ip = :last_ip WHERE user_id = :u_id LIMIT 1");
    $update_user_details->bindParam(':last_ip', $user_ip, PDO::PARAM_STR, 15);
    $update_user_details->bindParam(':u_id', $user_id, PDO::PARAM_INT);
    $update_user_details->execute();
    
    $GLOBALS["CURUSER"] = $row;
    
    }
    
    function is_logged_in() {
    global $CURUSER;
    if (!$CURUSER) {
    header("Location: domain.net/login.php?403");
    exit();
    }
    }
    
    ?>

     

    inc/pdo_conn.php

     

    <?php
    
    $db = new PDO('mysql:host=localhost;dbname=abc;charset=UTF-8', 'abc', 'xam');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    
    ?>

     

    until a few days ago it worked, but when i starting to expand my project

    i started to get this error

     

    PHP Fatal error: Call to a member function prepare() on a non-object in /root/inc/main.php"
    

     

    the error reffers to this line

    $fetch_user_details = $db->prepare("SELECT * FROM users WHERE user_id = :bitches_id LIMIT 1");
    

  11. @lemmin thank you very much for explaining me with so much detail

     

    but, you see this is what i did in the first place // before coming up with that ? statement : see

     

    the code was

    $htmlout = "<div>a</div>
    lalalalala
    <p style='color:red'>".$status."<br /></p>
    lalala";

     

    and then i realised if the form wasn't submitted i will get

    <p style='color:red'><br /></p>

    so that will be shown for no reason / so <p> that will be there for nothing cause its empty

     

    so that's why i chose that ternary operator

    if status didn't had a value - don't show <p> tag at all

    and if it did show the <p> tag with error or success message ...

     

    i hope that all of you, understood what's my malefic plan :D

    thanks for your time

     

     

  12. Eh, what? You can search INT fields...

    that's why i edited the post cause i tested my "advice"

    i replied like that cause i thought in a logical way / how can you search for a-Z in a numeric field / it's impossible to find because mysql doesn't accept text in int field / so why run the query ...

     

    Who said $search contains non-int characters? MySQL will just typecast it anyway.

    i presumed that backend user wont search for 1347289747

  13. foreach( array('lastname','firstname','email', 'message', 'phone') as $x )
    {
          ${$x} = $_POST[ $x ];
    }
    
    if ( !isset($_POST[ $x ]) || empty($message) || empty($email) )

     

    This makes very little sense. What are you trying to do here?

    for each value in the $array is created, $message $email etc ... // i guess you already knew this

    if the form isn't posted or message field is empty .. do that

     

    what i'm i missing ? :-\

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.