Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > BOOK: Professional PHP 5 ISBN: 978-0-7645-7282-1
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional PHP 5 ISBN: 978-0-7645-7282-1
This is the forum to discuss the Wrox book Professional PHP5 by Ed Lecky-Thompson, Heow Eide-Goodman, Steven D. Nowicki, Alec Cove; ISBN: 9780764572821
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional PHP 5 ISBN: 978-0-7645-7282-1 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old August 15th, 2005, 07:03 PM
Registered User
 
Join Date: Aug 2005
Location: , , USA.
Posts: 4
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to php4ever
Default Chapter 1 - Working Widget Code

I bought PHP5 with the great anticipation of it transforming this procedural programmer into an OOP one. That is happening but what needs even greater transformation is a reworking of many of the code examples. The authors smugly proclaim that they deem Postres as a professional database as if to say that MySQL is not. What the authors did was not so neatly sidestep the whole controversy regarding MySQL 4.x working with PHP5. I am sure there is more than one person on this planet who would have appreciated if that Widget Code found on p.13-15, had been written as more than a one-trick pony and with MySQL in mind for those of us who take pride in being LAMP professionals. Whichever of the guys who wrote the code also needed to lighten up and have a little fun with it. After all, if the fun isn't there, then why do it? So, I offer my own code inspired also by the errors in the original code that should have been caught by whoever QAed the code, assuming that it was QAed. So, hope the code I offer is of use to anyone who wants to learn how PHP5 and MySQL can work together in an OOP way:

CLASS.WIDGET.PHP

<?php



class Widget {
    private $id;
    private $_name;
    private $_description;
    private $hDB;
    private $needsUpdating = false;

    private $_host;
    private $_user;
    private $_pwd;

    public $db_selected=false;

    public function __construct($widgetID) {
        require("[here refer to your .inc.php file outside of docoument root that contains host,user,password]");
        //the widgetID param is the primar ke of a
        // record in th db containing the info
        // for this object.
        $this->setHost($host);
         $this->setUser($user);
         $this->setPwd($pwd);

        //create a connection handle and store it in a private member variable
        $this->hDB = mysql_connect($this->_host,$this->_user,$this->_pwd);
        if(! is_resource($this->hDB)) {
            throw new Exception('Unable to connect to the database.');
        }
echo "I can connect to database and get the following connection: ", get_resource_type($this->hDB);
    //exit(print_r($this->hDB));
        $this->db_selected = mysql_select_db('slevy1', $this->hDB);
        if (! $this->db_selected) {
           die ('Can\'t use slevy1 : ' . mysql_error());
        }
        $sql = "SELECT name,description FROM widget WHERE widgetid = $widgetID";
echo "<p>Here is my rock'n SQL ;)<BR><b> $sql</b></p>";
        $rs = mysql_query($sql, $this->hDB);
        if(! is_resource($rs)){
            throw new Exception('An error occurred selecting from the database.');
        }
        if (! mysql_num_rows($rs)) {
            throw new Exception('The specified widget does not exist!');
        }
        $data = mysql_fetch_array($rs);
        $this->id = $widgetID;
        $this->_name = $data['name'];
        $this->_description = $data['description'];
    }// end constructor

    public function getName() {
        return $this->_name;
    }

    public function getDescription() {
        return $this->_description;
    }

    public function setHost($h){
            $this->_host = $h;
    }
    public function setUser($u){
        $this->_user = $u;
    }

    public function setPwd($p){
        $this->_pwd = $p;
    }


    public function setName($name) {
        $this->_name = $name;
        $this->needsUpdating = true;
    }

    public function setDescription($description){
        $this->_description = $description;
        $this->needsUpdating = true;
    }

// DESTRUCTOR FUNCTION
    public function __destruct() {
        if (! $this->needsUpdating){
           mysql_close($this->hDB); // call me anal, but I like to close my database connections.
            return;
        } else {
            echo '<BR>Will try now to update things<BR>';
        }


    $sql = "UPDATE `widget` SET ";
    $sql .= "name = '" . mysql_escape_string($this->_name) . "', ";
    $sql .= "description = '" . mysql_escape_string($this->_description) . "' ";
    $sql .= " WHERE widgetID = " . $this->id;

echo '<p>IN: the __destruct() and here is the SQL<BR><b>', $sql,'</b><BR><BR>{Refresh page to see changes occur}</p>';

//echo get_resource_type($this->hDB),'<BR>';

if (! $this->db_selected) {
   die ('Can\'t use slevy1 : ' . mysql_error());
} else {
    // echo "<BR>$this->db_selected<BR>";
}



$retval = mysql_query($sql,$this->hDB); // mysql_query with UPDATE isn't going to return a recordset.
if (!$retval) {
    echo '<p>Whoopsie, Houston there is a problem as we could not update the database!</p>';
}
// you are done with the database so close the connection handle.
    mysql_close($this->hDB);

    }// end destructor
}

?>

=============================
TESTWIDGET.PHP

<?php
error_reporting(E_ALL);
require_once("class.Widget.php");

    try {
        $objWidget = new Widget(1);

        print "<p>Widget Name: " . $objWidget->getName() . "<br>\n";
        print "Widget Description: " . $objWidget->getDescription() . "<BR>\n</p>";

        if ($objWidget->getName() != 'BAR') {
            $objWidget->setName('BAR');
            $objWidget->setDescription('BARtacular,baby!');
        } else {
            $objWidget->setName('FOO');
            $objWidget->setDescription('FOOtacular,baby!');
        }

    } catch(Exception $e) {
        die("There was a problem: " . $e->getMessage());
    }

?>

Happy computing ...




Reply With Quote
The Following User Says Thank You to php4ever For This Useful Post:
loremipsum3891 (October 30th, 2009)
  #2 (permalink)  
Old January 13th, 2007, 10:40 PM
Registered User
 
Join Date: Jan 2007
Location: Perth, WA, Australia.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to ksparkar
Default

Have recently bought this book and I must say I am disappointed with the errors in the code. Thanks for the code php4ever.

Couple of clarifications for others who might be trying to get the code to work:

1. The PHP manual says that - "Note: Attempting to throw an exception from a destructor causes a fatal error" http://au.php.net/manual/en/language.oop5.decon.php - so I am not sure why the authors are attempting to do that in code in the book!

2. As already mentioned by php4ever - UPDATE does not return a recordset in MySQL hence 'is_resource($rs)' in the destructor will always evaluate to false if you are trying to get the code provided in the book to work on MySQL.

Cheers
Reply With Quote
  #3 (permalink)  
Old August 22nd, 2008, 06:06 AM
Registered User
 
Join Date: Aug 2007
Location: Great Missenden, Bucks, United Kingdom.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by php4ever
 I bought PHP5 with the great anticipation of it transforming this procedural programmer into an OOP one.  That is happening but what needs even greater transformation is a reworking of many of the code examples. The authors smugly proclaim that they deem Postres as a professional database as if to say that MySQL is not. What the authors did was not so neatly sidestep the whole controversy regarding MySQL 4.x working with PHP5. I am sure there is more than one person on this planet who would have appreciated if that Widget Code found on p.13-15, had been written as more than a one-trick pony and with MySQL in mind for those of us who take pride in being LAMP professionals. Whichever of the guys who wrote the code also needed to lighten up and have a little fun with it.  After all, if the fun isn't there, then why do it? So, I offer my own code inspired also by the errors in the original code that should have been caught by whoever QAed the code, assuming that it was QAed.  So, hope the code I offer is of use to anyone who wants to learn how PHP5 and MySQL can work together in an OOP way:

CLASS.WIDGET.PHP

<?php



class Widget {
    private $id;
    private $_name;
    private $_description;
    private $hDB;
    private $needsUpdating = false;

    private $_host;
    private $_user;
    private $_pwd;

    public $db_selected=false;

    public function __construct($widgetID) {
        require("[here refer to your .inc.php file outside of docoument root that contains host,user,password]");
        //the widgetID param is the primar ke of a
        // record in th db containing the info
        // for this object.
        $this->setHost($host);
         $this->setUser($user);
         $this->setPwd($pwd);

        //create a connection handle and store it in a private member variable
        $this->hDB = mysql_connect($this->_host,$this->_user,$this->_pwd);
        if(! is_resource($this->hDB)) {
            throw new Exception('Unable to connect to the database.');
        }
echo "I can connect to database and get the following connection: ", get_resource_type($this->hDB);  
    //exit(print_r($this->hDB));
        $this->db_selected = mysql_select_db('slevy1', $this->hDB);
        if (! $this->db_selected) {
           die ('Can\'t use slevy1 : ' . mysql_error());
        }
        $sql = "SELECT name,description FROM widget WHERE widgetid = $widgetID";
echo "<p>Here is my rock'n SQL ;)<BR><b> $sql</b></p>";
        $rs = mysql_query($sql, $this->hDB);
        if(! is_resource($rs)){
            throw new Exception('An error occurred selecting from the database.');
        }
        if (! mysql_num_rows($rs)) {
            throw new Exception('The specified widget does not exist!');
        }
        $data = mysql_fetch_array($rs);
        $this->id = $widgetID;
        $this->_name = $data['name'];
        $this->_description = $data['description'];
    }// end constructor

    public function getName() {
        return $this->_name;
    }

    public function getDescription() {
        return $this->_description;
    }

    public function setHost($h){
            $this->_host = $h;
    }
    public function setUser($u){
        $this->_user = $u;
    }

    public function setPwd($p){
        $this->_pwd = $p;
    }


    public function setName($name) {
        $this->_name = $name;
        $this->needsUpdating = true;
    }

    public function setDescription($description){
        $this->_description = $description;
        $this->needsUpdating = true;
    }

// DESTRUCTOR FUNCTION
    public function __destruct() {
        if (! $this->needsUpdating){
           mysql_close($this->hDB);    // call me anal, but I like to close my database connections.
            return;
        } else {
            echo '<BR>Will try now to update things<BR>';
        }


    $sql = "UPDATE `widget` SET ";
    $sql .= "name = '" . mysql_escape_string($this->_name) . "',  ";
    $sql .= "description = '" . mysql_escape_string($this->_description) . "' ";
    $sql .= " WHERE widgetID = " . $this->id;

echo '<p>IN: the __destruct() and here is the SQL<BR><b>', $sql,'</b><BR><BR>{Refresh page to see changes occur}</p>';

//echo get_resource_type($this->hDB),'<BR>';

if (! $this->db_selected) {
   die ('Can\'t use slevy1 : ' . mysql_error());
} else {
    // echo "<BR>$this->db_selected<BR>";
}



$retval = mysql_query($sql,$this->hDB); // mysql_query with UPDATE isn't going to return a recordset.
if (!$retval) {
    echo '<p>Whoopsie, Houston there is a problem as we could not update the database!</p>';
}
// you are done with the database so close the connection handle.
    mysql_close($this->hDB);

    }// end destructor
}

?>

=============================
TESTWIDGET.PHP

<?php
error_reporting(E_ALL);
require_once("class.Widget.php");

    try {
        $objWidget = new Widget(1);

        print "<p>Widget Name: " . $objWidget->getName() . "<br>\n";
        print "Widget Description: " . $objWidget->getDescription() . "<BR>\n</p>";

        if ($objWidget->getName() != 'BAR') {
            $objWidget->setName('BAR');
            $objWidget->setDescription('BARtacular,baby!');
        } else {
            $objWidget->setName('FOO');
            $objWidget->setDescription('FOOtacular,baby!');
        }

    } catch(Exception $e) {
        die("There was a problem: " . $e->getMessage());
    }

?>

Happy computing ...




Reply With Quote
  #4 (permalink)  
Old August 22nd, 2008, 06:51 AM
Registered User
 
Join Date: Aug 2007
Location: Great Missenden, Bucks, United Kingdom.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Re PHP5 Professional, have you progressed to chapter 3 yet?

I've just got there, tried the test.php script and get the response

Fatal error: Access type for interface method Validator::validate() must be omitted in ...\contactManager\interface.Validator.php on line 3

interface.Validator.php consists of 5 lines :-

<?php
  interface Validator {
    abstract function validate();
  }
?>

Difficult to see anything obviously wrong and equally difficult to understand the error message.

Any bright ideas?

Thanks

Ivan


Reply With Quote
  #5 (permalink)  
Old July 17th, 2009, 12:07 AM
Registered User
 
Join Date: Jun 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hey Ivan,

I just ran into that myself, i think it may have something with function validate() within the class.Entity.php file, i may be wrong though. Pls let me know the solution if you have already figured it out? Im going to have a go now.

Cheers, Jean
Reply With Quote
  #6 (permalink)  
Old September 5th, 2013, 03:16 PM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2013
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

does not work!!!!!!

require("[here refer to your .inc.php file outside of docoument root that contains host,user,password]"); writing "require "widget.inc.php";" where
<?php
$host = "....";
$user = "...";
$pwd = "";
?>
displays: I can connect to database and get the following connection: mysql linkResource id #51
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 3 - a:visited not working VeganMan BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 4 April 3rd, 2008 01:31 AM
Chapter 4 - Two Views, not working for me... bigtalk BOOK: Beginning Ruby on Rails 1 January 3rd, 2008 07:30 AM
Chapter 1, First App, No, it's not working redthor BOOK: Beginning Ruby on Rails 2 February 1st, 2007 08:32 PM
new Ajax article: Creating an Ajax Search Widget jminatel Ajax 0 May 11th, 2006 03:50 PM
Working Code for Chapter 4 clomby BOOK: Professional PHP 5 ISBN: 978-0-7645-7282-1 1 December 26th, 2005 06:44 AM



All times are GMT -4. The time now is 09:18 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.