php-general Digest 16 Feb 2013 20:30:37 -0000 Issue 8125

Topics (messages 320227 through 320229):

Re: Newbie is trying to set up OOP With PHP and MySQL or MySQLi database class 
(using CRUD)
        320227 by: dealTek
        320228 by: David Robley

Re: webDAV/CalDAV client class experience ?
        320229 by: B. Aerts

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
Thanks for all the help folks,


PHP-light-PDO-Class

ok well I found this...

https://github.com/poplax/PHP-light-PDO-Class

But it does not seem to recognize the port - I put the port as 8889 but keeps 
saying can't connect port 3306

Warning: PDO::__construct() [pdo.--construct]: [2002] Connection refused 
(trying to connect via tcp://127.0.0.1:3306) in 
/Users/revdave/Sites/php-fool/pdo3/PHP-light-PDO-Class-master/class.lpdo.php on 
line 33
Connection failed: SQLSTATE[HY000] [2002] Connection refused

BTW: I tried to add the port a few places but it didn't work......


How do we fix this?



------ config.php

<?php
$config = array();
$config['Database'] = array();
$config['Database']['dbtype'] = 'mysql';
$config['Database']['dbname'] = 'tester';
$config['Database']['host'] = '127.0.0.1';
$config['Database']['port'] = 8889;
$config['Database']['username'] = 'root';
$config['Database']['password'] = 'root';
$config['Database']['charset'] = 'utf8';
?>

===============  class.lpdo.php


<?php
/**
 * 
 * @Author : Poplax [Email:linjiang9...@gmail.com]; 
 * @Date : Fri Jun 03 10:17:17 2011;
 * @Filename class.lpdo.php;
 */

/**
 * class lpdo PDO
 * one table support only
 */
class lpdo extends PDO
{
        public $sql = '';
        public $tail = '';
        private $charset = 'UTF8';
        private $options;

        /**
         * 
         * @Function : __construct;
         * @Param  $ : $options Array DB config ;
         * @Return Void ;
         */
        public function __construct($options)
        {
                $this->options = $options;
                $dsn = $this->createdsn($options);
                $attrs = empty($options['charset']) ? 
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES " . $this->charset) : 
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES " . $options['charset']);
                try
                {
                        parent::__construct($dsn, $options['username'], 
$options['password'], $attrs);
                }
                catch (PDOException $e)
                {
                        echo 'Connection failed: ' . $e->getMessage();
                }
        }

        /**
         * 
         * @Function : createdsn;
         * @Param  $ : $options Array;
         * @Return String ;
         */
        private function createdsn($options)
        {
                return $options['dbtype'] . ':host=' . $options['host'] . 
';dbname=' . $options['dbname'];
        }

        /**
         * 
         * @Function : get_fields;
         * @Param  $ : $data Array;
         * @Return String ;
         */
        private function get_fields($data)
        {
                $fields = array();
                if (is_int(key($data)))
                {
                        $fields = implode(',', $data);
                }
                else if (!empty($data))
                {
                        $fields = implode(',', array_keys($data));
                }
                else
                {
                        $fields = '*';
                }
                return $fields;
        }

        /**
         * 
         * @Function : get_condition;
         * @Param  $ : $condition Array, $oper String, $logc String;
         * @Return String ;
         */
        private function get_condition($condition, $oper = '=', $logc = 'AND')
        {
                $cdts = '';
                if (empty($condition))
                {
                        return $cdts = '';
                }
                else if (is_array($condition))
                {
                        $_cdta = array();
                        foreach($condition as $k => $v)
                        {
                                if (!is_array($v))
                                {
                                        if (strtolower($oper) == 'like')
                                        {
                                                $v = '\'%' . $v . '%\'';
                                        }
                                        else if (is_string($v))
                                        {
                                                $v = '\'' . $v . '\'';
                                        }
                                        $_cdta[] = ' ' . $k . ' ' . $oper . ' ' 
. $v . ' ' ;
                                }
                                else if (is_array($v))
                                {
                                        $_cdta[] = $this->split_condition($k, 
$v);
                                }
                        }
                        $cdts .= implode($logc, $_cdta);
                }
                return $cdts;
        }

        /**
         * 
         * @Function : split_condition;
         * @Param  $ : $field String, $cdt Array;
         * @Return String ;
         */
        private function split_condition($field, $cdt)
        {
                $cdts = array();
                $oper = empty($cdt[1]) ? '=' : $cdt[1];
                $logc = empty($cdt[2]) ? 'AND' : $cdt[2];
                if (!is_array($cdt[0]))
                {
                        $cdt[0] = is_string($cdt[0]) ? "'$cdt[0]'" : $cdt[0];
                }
                else if (is_array($cdt[0]) || strtoupper(trim($cdt[1])) == 'IN')
                {
                        $cdt[0] = '(' . implode(',', $cdt[0]) . ')';
                }

                $cdta[] = " $field $oper {$cdt[0]} ";
                if (!empty($cdt[3]))
                {
                        $cdta[] = $this->get_condition($cdt[3]);
                }
                $cdts = ' ( ' . implode($logc, $cdta) . ' ) ';
                return $cdts;
        }

        /**
         * 
         * @Function : get_fields_datas;
         * @Param  $ : $data Array;
         * @Return Array ;
         */
        private function get_fields_datas($data)
        {
                $arrf = $arrd = array();
                foreach($data as $f => $d)
                {
                        $arrf[] = '`' . $f . '`';
                        $arrd[] = is_string($d) ? '\'' . $d . '\'' : $d;
                }
                $res = array(implode(',', $arrf), implode(',', $arrd));
                return $res;
        }

        /**
         * 
         * @Function : get_rows;
         * @Param  $ : $table String, $getRes Boolean, $condition Array, 
$column Array;
         * @Return Array |Object;
         */
        public function get_rows($table, $condition = array(), $getRes = false, 
$column = array())
        {
                $fields = $this->get_fields($column);
                $cdts = $this->get_condition($condition);
                $where = empty($condition) ? '' : ' where ' . $cdts;
                $this->sql = 'select ' . $fields . ' from ' . $table . $where;
                try
                {
                        $this->sql .= $this->tail;
                        $rs = parent::query($this->sql);
                }
                catch(PDOException $e)
                {
                        trigger_error("get_rows: ", E_USER_ERROR);
                        echo $e->getMessage() . "<br/>\n";
                }
                $rs = $getRes ? $rs : $rs->fetchAll(parent::FETCH_ASSOC);
                return $rs;
        }

        /**
         * 
         * @Function : get_all;
         * @Param  $ : $table String, $condition Array, $getRes Boolean;
         * @Return Array |Object;
         */
        public function get_all($table, $getRes = false, $condition = array())
        {
                return $this->get_rows($table, $condition, $getRes);
        }

        /**
         * 
         * @Function : get_one;
         * @Param  $ : $table String, $condition Array, $getRes Boolean, 
$column Array;
         * @Return Array ;
         */
        public function get_one($table, $condition = array(), $column = array())
        {
                $rs = $this->get_rows($table, $condition, true, $column);
                $rs = $rs ? $rs->fetch(parent::FETCH_ASSOC) : $rs;
                return $rs;
        }

        /**
         * 
         * @Function : insert;
         * @Param  $ : $table String, $data Array;
         * @Return Int ;
         */
        public function insert($table, $data)
        {
                list($strf, $strd) = $this->get_fields_datas($data);
                $this->sql = 'insert into `' . $table . '` (' . $strf . ') 
values (' . $strd . '); ';
                return $this->exec($this->sql, __METHOD__);
        }

        /**
         * 
         * @Function : update;
         * @Param  $ : $table String, $data Array, $condition Array;
         * @Return Int ;
         */
        public function update($table, $data, $condition)
        {
                $cdt = $this->get_condition($condition);
                $arrd = array();
                foreach($data as $f => $d)
                {
                        $arrd[] = "`$f` = '$d'";
                }
                $strd = implode(',', $arrd);
                $this->sql = 'update ' . $table . ' set ' . $strd . ' where ' . 
$cdt;
                return $this->exec($this->sql, __METHOD__);
        }

        /**
         * 
         * @Function : save;
         * @Param  $ : $table String, $data Array, $condition Array;
         * @Return Int ;
         */
        public function save($table, $data, $condition = array())
        {
                $cdt = $this->get_condition($condition);
                list($strf, $strd) = $this->get_fields_datas($data);
                $has1 = $this->get_one($table, $condition);
                if (!$has1)
                {
                        $enum = $this->insert($table, $data);
                }
                else
                {
                        $enum = $this->update($table, $data, $condition);
                }
                return $enum;
        }

        /**
         * 
         * @Function : delete;
         * @Param  $ : $table String, $condition Array;
         * @Return Int ;
         */
        public function delete($table, $condition)
        {
                $cdt = $this->get_condition($condition);
                $this->sql = 'delete from ' . $table . ' where ' . $cdt;
                return $this->exec($this->sql, __METHOD__);
        }

        /**
         * 
         * @Function : exec;
         * @Param  $ : $sql, $method;
         * @Return Int ;
         */
        public function exec($sql, $method = '')
        {
                try
                {
                        $this->sql = $sql . $this->tail;
                        $efnum = parent::exec($this->sql);
                }
                catch(PDOException $e)
                {
                        echo 'PDO ' . $method . ' Error: ' . $e->getMessage();
                }
                return intval($efnum);
        }

        /**
         * 
         * @Function : setLimit;
         * @Param  $ : $start, $length;
         * @Return ;
         */
        public function set_limit($start = 0, $length = 20)
        {
                $this->tail = ' limit ' . $start . ', ' . $length;
        }
}
?>



--
Thanks,
Dave - DealTek
deal...@gmail.com
[db-3]











--- End Message ---
--- Begin Message ---
dealTek wrote:

> 
> Thanks for all the help folks,
> 
> 
> PHP-light-PDO-Class
> 
> ok well I found this...
> 
> https://github.com/poplax/PHP-light-PDO-Class
> 
> But it does not seem to recognize the port - I put the port as 8889 but
> keeps saying can't connect port 3306
> 
> Warning: PDO::__construct() [pdo.--construct]: [2002] Connection refused
> (trying to connect via tcp://127.0.0.1:3306) in
> /Users/revdave/Sites/php-fool/pdo3/PHP-light-PDO-Class-
master/class.lpdo.php
> on line 33 Connection failed: SQLSTATE[HY000] [2002] Connection refused
> 
> BTW: I tried to add the port a few places but it didn't work......
> 
> 
> How do we fix this?
> 
> 
> 
> ------ config.php
> 
> <?php
> $config = array();
> $config['Database'] = array();
> $config['Database']['dbtype'] = 'mysql';
> $config['Database']['dbname'] = 'tester';
> $config['Database']['host'] = '127.0.0.1';
> $config['Database']['port'] = 8889;
> $config['Database']['username'] = 'root';
> $config['Database']['password'] = 'root';
> $config['Database']['charset'] = 'utf8';
> ?>

Change host to localhost - your mysql may be configured not to accept 
requests via tcp.

> 
> ===============  class.lpdo.php
<SNIP>
> 
> 
> --
> Thanks,
> Dave - DealTek
> deal...@gmail.com
> [db-3]

-- 
Cheers
David Robley

My karma ran over my dogma


--- End Message ---
--- Begin Message ---
On 13/02/13 14:27, Daniel Brown wrote:
On Tue, Feb 12, 2013 at 3:40 PM, B. Aerts <ba_ae...@yahoo.com> wrote:
Hello,

I'm working on this one for more than a year (personal project) - but I'm
turning pretty desperate here.

I'm trying to connect to 2 Calendars through the CalDAV protocol.
The calendars are hosted by 2 webmail providers.
If I try to sync through a dedicated calendar, like iCal or Thunderbird
Lightning add-on, this works fine.

However, once I try to do it through "native" PHP, I fail miserably - even
if I mimick HTTP requests as recorded by Charles (HTTP debugging proxy).

Up until now, I used the inc_caldav-client-v2.php, which worked for a while
and then stopped all of a sudden. The PUT requests failed, and then any HTTP
request got caught in what appears to be a socket timeout.

My question: is anyone using some webDAV/CalDAV class that actually works ?
If not, any tutorial on the subject is also deeply appreciated ( all I can
rely on is the IETF spec rfc4791, which is far from accesible reading
material)

     I haven't tried them myself, but there are PEAR packages for
client-server implementations for WebDAV:

         http://pear.php.net/search.php?q=webdav&in=packages&x=0&y=0

     They're not actively maintained by anyone right now (feel free to
apply to change that if you'd like the responsibility), but the most
recent server version was released just this past October (the client
version is about a year older).  At the very least, it may be enough
to get you started.


Hello,

I did try to download the PEAR package, but got stuck in some missing header problem. In the mean time however, I did manage to solve a few problems - listed for archiving's sake : - the biggest mistake: apparently I commented the fwrite() call to the stream, which explains why he went in time-out ... (in this case, please DO shoot the pianist)

- CalDAVClient class from inc_caldav-client-v2.php always adds the port to the "Host" HTTP header, even if it is the default port; by removing it from the header, I got less problems - Adding the HTTP header "Accept: */*" made sure all read actions ( e.g. GET, PROPFIND, REPORT) worked perfectly

Only problem remaining was that PUT still isn't possible - at least not with one of the providers. Since I used a verbatim copy of a PUT action from the RFC, I strongly suspect the problem to be with the provider.

Thanks for the help !

Bert


--- End Message ---

Reply via email to