/    Sign up×
Community /Pin to ProfileBookmark

Learning OOP for the first time

Hello

I am new in oop. I am reading Professional PHP5 of WROX. It uses Postgre as database and i am using Mysql. Whatever, I found a code and changed it to mysql. Here it is :

// class.Widget.php
<?php

class Widget {

private $id;
private $name;
private $description; private $hDB;
private $needsUpdating = false;

public function __construct($widgetID) {
//The widgetID parameter is the primary key of a
//record in the database containing the information
//for this object

//Create a connection handle and store it in a private member variable
$this->hDB = mysql_connect (‘dbname=oop user=root’);
if(! is_resource($this->hD?) {
throw new Exception(‘Unable to connect to the database.’);
}

$sql = “SELECT “name”, “description” FROM widget WHERE widgetid = $widgetID”;
$rs = mysql_query($this->hDB, $sql);
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’];

}

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

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

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

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

public function __destruct() {
if(! $this->needsUpdating) {
return;
}

$sql = ‘UPDATE “widget” SET ‘;
$sql .= “”name” = ‘” . mysql_escape_string($this->name) . “‘, “;
$sql .= “”description” = ‘” . pg_escape_string($this->description) . “‘ “;
$sql .= “WHERE widgetID = ” . $this->id;

$rs = mysql_query($this->hDB, $sql);
if(! is_resource($rs)) {
throw new Exception(‘An error occurred updating the database’);
}

//You’re done with the database. Close the connection handle.
mysql_close($this->hD?;
}
}
?>

And another file called testWidget.php is :

<?php

require_once(‘class.Widget.php’);

try {
$objWidget = new Widget(1);

print “Widget Name: ” . $objWidget->getName() . “<br>n”;
print “Widget Description: ” . $objWidget->getDescription() . “<br>n”;

$objWidget->setName(‘Bar’);
$objWidget->setDescription(‘This is a bartacular widget!’);
} catch (Exception $e) {
die(“There was a problem: ” . $e->getMessage());
}

?>

There is a datebase name oop and the structure is

CREATE TABLE widget (
“widgetid” SERIAL PRIMARY KEY NOT NULL,
“name” varchar(255) NOT NULL,
“description” text
);
INSERT INTO widget (“name”, “description”)
VALUES(‘Foo’, ‘This is a footacular widget!’);

When I run testWidget.php the following error occurs:

“Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL server host ‘dbname=oop user=root’ (11004) in E:apachexampphtdocsLearningclass.Widget.php on line 16
There was a problem: Unable to connect to the database.”.

Can anyone help me?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@webnauthorJun 20.2006 — I have changed class.Widget.php to :
[code=php]
<?php

class Widget {

private $id;
private $name;
private $description;

private $hSERVER;

private $hDB;
private $needsUpdating = false;

public function __construct($widgetID) {
//The widgetID parameter is the primary key of a
//record in the database containing the information
//for this object

//Create a connection handle and store it in a private member variable
$this->hSERVER = mysql_connect ("localhost", "root", "");
if(! is_resource($this->hSERVER)) {
throw new Exception('Unable to connect to the server.');
}
$this->hDB = mysql_select_db ("oop");
if(! is_resource($this->hDB)) {
throw new Exception('Unable to connect to the database.');
}

$sql = "SELECT "name", "description" FROM widget WHERE widgetid = $widgetID";
$rs = mysql_query($this->hDB, $sql);
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'];

}

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

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

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

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

public function __destruct() {
if(! $this->needsUpdating) {
return;
}

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

$rs = mysql_query($this->hDB, $sql);
if(! is_resource($rs)) {
throw new Exception('An error occurred updating the database');
}

//You're done with the database. Close the connection handle.
mysql_close($this->hDB);
}
}
?>
[/code]

And get the following problem :

There was a problem: Unable to connect to the database
Copy linkTweet thisAlerts:
@NogDogJun 20.2006 — Does the "root" mysql account not have a password? Your code shows an empty string as the password in your call to mysql_connect(). (Don't show us the password, I just want to make sure it's supposed to be empty.)
Copy linkTweet thisAlerts:
@webnauthorJun 21.2006 — Here is my latest code
[code=php]
<?php

class Widget {

private $id;
private $name;
private $description;

//private $hSERVER;

private $hDB;
private $needsUpdating = false;

public function __construct($widgetID) {
//The widgetID parameter is the primary key of a
//record in the database containing the information
//for this object
//Create a connection handle and store it in a private member variable
/*
$this->hSERVER = mysql_connect ("localhost", "root", "")

or throw new Exception('Unable to connect to the server.');
*/

$this->hSERVER = mysql_connect ("localhost", "root", "");
if(! $this->hSERVER ) {
throw new Exception('Unable to connect to the server.');
}


$dbconn = mysql_connect( "localhost", "root", "" );
$this->hDB = mysql_select_db ("oop");
if(!$this->hDB) {
throw new Exception('Unable to connect to the database.');
}

$sql = "SELECT name, description FROM widget WHERE widgetid = $widgetID";
$rs = mysql_query($sql);
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'];

}

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

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

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

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

public function __destruct() {
if(! $this->needsUpdating) {
return;
}

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

$rs = mysql_query($sql);
if(! is_resource($rs)) {
throw new Exception('An error occurred updating the database');
}

//You're done with the database. Close the connection handle.
mysql_close($this->hDB);
}
}

?>
and output shows:
[code=php]
Widget Name: Bar
Widget Description: This is a bartacular widget!

Fatal error: Exception thrown without a stack frame in Unknown on line 0
[/code]


So, it interacts with database. Ok. But, Why this line ?
[code=php]
Fatal error: Exception thrown without a stack frame in Unknown on line 0
[/code]


Can anyone help me ?
×

Success!

Help @webn spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...