php-general Digest 10 Feb 2008 11:59:02 -0000 Issue 5285

Topics (messages 269014 through 269022):

Better DB Class MySQL
        269014 by: nihilism machine
        269015 by: Larry Garfield
        269016 by: Nathan Nobbe
        269017 by: Robert Cummings

urgent !!! Please Help preg_replace !!!!!!!!!!!!!!!!!!!!!
        269018 by: LKSunny
        269019 by: Robert Cummings
        269020 by: LKSunny

strtotime
        269021 by: Ron Piggott
        269022 by: Ron Piggott

Administrivia:

To subscribe to the digest, e-mail:
        [EMAIL PROTECTED]

To unsubscribe from the digest, e-mail:
        [EMAIL PROTECTED]

To post to the list, e-mail:
        [EMAIL PROTECTED]


----------------------------------------------------------------------
--- Begin Message --- Looking to really beef up my DB class, any suggestions for functions to add that will be more time saving for a web 2.0 app, or ways to improve existing methods? thank you everyone in advance.

<?php

class db {

        //      Members
        public $db_user = "";
        public $db_pass = "";
        public $db_name = "";
        public $db_server = "";
        public $link;
        public $result_id;

        //      Methods
        public function __construct() {
                $this->connect();
        }

        // Connect to MySQL Server
        public function connect() {
$this->link = mysql_connect($this->db_server,$this->db_user,$this- >db_pass) or die("Error: Cannot Connect to DataBase"); mysql_select_db($this->db_name,$this->link) or die("Error: Cannot Select Database (" . $this->db_name . ")");
        }

        // MySQL Query
        public function query($sql) {
                $this->result_id = mysql_query($sql);
                return $this->fetch_rows();
        }       

        // MySQL Query
        public function insert($sql) {
                $this->result_id = mysql_query($sql);
                return $this->select_id;
        }
                
        // MySQL Fetch Rows
        public function fetch_rows() {
                $rows = array();
                if($this->result_id){
                        while($row = mysql_fetch_object($this->result_id)) {
                                $rows[] = $row;
                        }               
                }
                return $rows;   
        }

        // MySQL Affected Rows
        public function num_rows() {
                return mysql_num_rows($this->link);
        }
        
        // MySQL Affected Rows
        public function select_id() {
                return mysql_insert_id($this->link);
        }

        // Disconnect from MySQL Server
        public function disconnect() {
                mysql_close($this->link);
        }

        // Terminator Style Function simply in coolness
        public function Terminator($tbl) {
        }
        
        // Destruct!
        public function __destruct() {
                $this->disconnect();
        }
}

?>

--- End Message ---
--- Begin Message ---
http://www.php.net/pdo

All the cool kids are doing it.

On Saturday 09 February 2008, nihilism machine wrote:
> Looking to really beef up my DB class, any suggestions for functions
> to add that will be more time saving for a web 2.0 app, or ways to
> improve existing methods? thank you everyone in advance.
>
> <?php
>
> class db {
>
>       //      Members
>       public $db_user = "";
>       public $db_pass = "";
>       public $db_name = "";
>       public $db_server = "";
>       public $link;
>       public $result_id;
>
>       //      Methods
>       public function __construct() {
>               $this->connect();
>       }
>
>       // Connect to MySQL Server
>       public function connect() {
>               $this->link = 
> mysql_connect($this->db_server,$this->db_user,$this-
>
>  >db_pass) or die("Error: Cannot Connect to DataBase");
>
>               mysql_select_db($this->db_name,$this->link) or die("Error: 
> Cannot
> Select Database (" . $this->db_name .  ")");
>       }
>
>       // MySQL Query
>       public function query($sql) {
>               $this->result_id = mysql_query($sql);
>               return $this->fetch_rows();
>       }
>
>       // MySQL Query
>       public function insert($sql) {
>               $this->result_id = mysql_query($sql);
>               return $this->select_id;
>       }
>
>       // MySQL Fetch Rows
>       public function fetch_rows() {
>               $rows = array();
>               if($this->result_id){
>                       while($row = mysql_fetch_object($this->result_id)) {
>                               $rows[] = $row;
>                       }
>               }
>               return $rows;
>       }
>
>       // MySQL Affected Rows
>       public function num_rows() {
>               return mysql_num_rows($this->link);
>       }
>
>       // MySQL Affected Rows
>       public function select_id() {
>               return mysql_insert_id($this->link);
>       }
>
>       // Disconnect from MySQL Server
>       public function disconnect() {
>               mysql_close($this->link);
>       }
>
>       // Terminator Style Function simply in coolness
>       public function Terminator($tbl) {
>       }
>
>       // Destruct!
>       public function __destruct() {
>               $this->disconnect();
>       }
> }
>
> ?>


-- 
Larry Garfield                  AIM: LOLG42
[EMAIL PROTECTED]               ICQ: 6817012

"If nature has made any one thing less susceptible than all others of 
exclusive property, it is the action of the thinking power called an idea, 
which an individual may exclusively possess as long as he keeps it to 
himself; but the moment it is divulged, it forces itself into the possession 
of every one, and the receiver cannot dispossess himself of it."  -- Thomas 
Jefferson

--- End Message ---
--- Begin Message ---
On Feb 9, 2008 7:03 PM, nihilism machine <[EMAIL PROTECTED]> wrote:

> Looking to really beef up my DB class, any suggestions for functions
> to add that will be more time saving for a web 2.0 app, or ways to
> improve existing methods? thank you everyone in advance.


first of all, as a matter of style, its conventional to capitalize class
names.  so
you would change the name to Db.  also, conventionally, member functions are
not capitalized; so Terminator(), becomes terminator().

next thing is you have no escaping of the queries.  you should be wrapping
the
$sql that gets passed into your query method in mysql_real_escape_string().

the insert() method appears to have a problem;
       public function insert($sql) {
               $this->result_id = mysql_query($sql);
               return $this->select_id;
       }
namely, its returning an instance variable that isnt set anywhere else.

i see no method named update() or delete() (or drop() [although im not sure
if
you really need that]).  but im guessing you are probly using insert() for
these
sorts of queries.  you should either create these methods and have them use
a
common function w/ private access that does the query and stores
mysql_affected_rows(), or at the very least rename insert() to something
more
appropriate.

im not sure why you would name a call to mysql_insert_id(), select_id(); it
seems
better to preserve the original name.

you might also want methods that allow iteration over the result set, rather
than
just a single method which buffers the entire result set into an array and
returns it.

-nathan

--- End Message ---
--- Begin Message ---
On Sat, 2008-02-09 at 18:39 -0600, Larry Garfield wrote:
> http://www.php.net/pdo
> 
> All the cool kids are doing it.

I always told the cool kids to kiss my buttocks. You too can set
yourself apart from the sheeple >:)

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

--- End Message ---
--- Begin Message ---
<?
//please run this script, you can get what problem i got

$html = <<<eof
<a href="aaa.html">aaa</a>

<a href="bbb.html">cfdfd</a>

<a href="aaa.html">sfs
sfsrbbb
sfds</a>

<a href="aaa.html">cc
gd
c</a>

<a href="aaa.html">ddd
123</a>
eof;

/*
i want replace any |<a.has bbb.</a>|, like
<a href="bbb.html">cfdfd</a>

<a href="aaa.html">sfs
sfsrbbb
sfds</a>
*/


$s[] = "|<a.*?(?!</a>).*?bbb.*?</a>|si";
$r[] = "X";

print preg_replace($s, $r, $html);
?> 

--- End Message ---
--- Begin Message ---
On Sun, 2008-02-10 at 14:33 +0800, LKSunny wrote:
> //please run this script, you can get what problem i got
> 
> $html = <<<eof
> <a href="aaa.html">aaa</a>
> 
> <a href="bbb.html">cfdfd</a>
> 
> <a href="aaa.html">sfs
> sfsrbbb
> sfds</a>
> 
> <a href="aaa.html">cc
> gd
> c</a>
> 
> <a href="aaa.html">ddd
> 123</a>
> eof;
> 
> /*
> i want replace any |<a.has bbb.</a>|, like
> <a href="bbb.html">cfdfd</a>
> 
> <a href="aaa.html">sfs
> sfsrbbb
> sfds</a>
> */
> 

<?php

$reps = array
(
    array
    (
        'match'   => '#<a.*</a>#Uims',
        'check'   => '#bbb#',
        'replace' => 'X',
    ),
);

foreach( $reps as $criteria )
{
    if( preg_match_all( $criteria['match'], $html, $matches ) )
    {
        foreach( $matches[0] as $match )
        {
            if( preg_match( $criteria['check'], $match ) )
            {
                $html = str_replace( $match, $criteria['replace'], $html );
            }
        }
    }
}

?>

This does what you want to $html, but it doesn't do it in a single
regex. I'm not sure it can be done with a single regex, and if it can, I
doubt it's simple. I don't feel like investigating too far :)

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'


--- End Message ---
--- Begin Message ---
this i know already, but i think can do it single regex

"Robert Cummings" <[EMAIL PROTECTED]> ¼¶¼g©ó¶l¥ó·s»D:[EMAIL PROTECTED]
>
> On Sun, 2008-02-10 at 14:33 +0800, LKSunny wrote:
>> //please run this script, you can get what problem i got
>>
>> $html = <<<eof
>> <a href="aaa.html">aaa</a>
>>
>> <a href="bbb.html">cfdfd</a>
>>
>> <a href="aaa.html">sfs
>> sfsrbbb
>> sfds</a>
>>
>> <a href="aaa.html">cc
>> gd
>> c</a>
>>
>> <a href="aaa.html">ddd
>> 123</a>
>> eof;
>>
>> /*
>> i want replace any |<a.has bbb.</a>|, like
>> <a href="bbb.html">cfdfd</a>
>>
>> <a href="aaa.html">sfs
>> sfsrbbb
>> sfds</a>
>> */
>>
>
> <?php
>
> $reps = array
> (
>    array
>    (
>        'match'   => '#<a.*</a>#Uims',
>        'check'   => '#bbb#',
>        'replace' => 'X',
>    ),
> );
>
> foreach( $reps as $criteria )
> {
>    if( preg_match_all( $criteria['match'], $html, $matches ) )
>    {
>        foreach( $matches[0] as $match )
>        {
>            if( preg_match( $criteria['check'], $match ) )
>            {
>                $html = str_replace( $match, $criteria['replace'], $html );
>            }
>        }
>    }
> }
>
> ?>
>
> This does what you want to $html, but it doesn't do it in a single
> regex. I'm not sure it can be done with a single regex, and if it can, I
> doubt it's simple. I don't feel like investigating too far :)
>
> Cheers,
> Rob.
> -- 
> .------------------------------------------------------------.
> | InterJinn Application Framework - http://www.interjinn.com |
> :------------------------------------------------------------:
> | An application and templating framework for PHP. Boasting  |
> | a powerful, scalable system for accessing system services  |
> | such as forms, properties, sessions, and caches. InterJinn |
> | also provides an extremely flexible architecture for       |
> | creating re-usable components quickly and easily.          |
> `------------------------------------------------------------'
> 

--- End Message ---
--- Begin Message ---
I am trying to calculate what was the date 18 months ago.  When I give
the command:

$18_months_ago = strtotime("-18 months");

It comes back with: 

Parse error: parse error, unexpected T_LNUMBER, expecting T_VARIABLE

How would you calculate 18 months ago from today?

Ron

--- End Message ---
--- Begin Message ---
I see I broke a rule.  The variable can't start with a number.  Still
strtotime doesn't work with -18 months   How would you handle this?  Ron

On Sun, 2008-02-10 at 06:46 -0500, Ron Piggott wrote:
> I am trying to calculate what was the date 18 months ago.  When I give
> the command:
> 
> $18_months_ago = strtotime("-18 months");
> 
> It comes back with: 
> 
> Parse error: parse error, unexpected T_LNUMBER, expecting T_VARIABLE
> 
> How would you calculate 18 months ago from today?
> 
> Ron
-- 
[EMAIL PROTECTED] 
www.actsministrieschristianevangelism.org 

Acts Ministries Christian Evangelism 
"Where People Matter" 
12 Burton Street 
Belleville, Ontario, Canada   K8P 1E6 
 
In Belleville Phone : (613) 967-0032 
In North America Call Toll Free : (866) ACTS-MIN 
Fax: (613) 967-9963 

--- End Message ---

Reply via email to