Re: [PHP] Newbie is trying to set up OOP With PHP and MySQL or MySQLi database class (using CRUD)

2013-02-15 Thread dealTek

On Feb 14, 2013, at 11:46 AM, Bastien Koert  wrote:

> 
> 
> The DreamInCode one is good. MySQLi is the recommended option over
> MySQL since the mysql one is deprecated. PDO is also a great option
> since you can prepare your queries to help sanitize the data
> 
> -- 
> 
> Bastien



Thanks Bastien,

I imagine this is what you were referring to?

http://www.dreamincode.net/forums/topic/54239-introduction-to-mysqli-and-prepared-statements/

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



[PHP] Re: Newbie is trying to set up OOP With PHP and MySQL or MySQLi database class (using CRUD)

2013-02-15 Thread dealTek

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



===  class.lpdo.php


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)

[PHP] Re: Newbie is trying to set up OOP With PHP and MySQL or MySQLi database class (using CRUD)

2013-02-15 Thread David Robley
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
> 
>  $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

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

-- 
Cheers
David Robley

My karma ran over my dogma


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php