Help with change postgreSQL connection to MySQL
I am currently reading Professional PHP 5 and I have only used MySQL and was wondering if anyone could help me figure out the connection line. I did it the way I thought would work but its not working.
Here is my code:
<?php
class Widget
{
private $id;
private $name;
private $description;
private $db;
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->db = @mysql_connect('localhost', 'root', '');
//$this->db = mysql_connect('localhost', stgres password=root ");
if (! is_resource($this->db))
{
exit('<p>Unable to connect to the ' . 'database server at this time.</p>');
}
if (!@mysql_select_db('practicephpdb'))
{
exit('<p>Unable to locate the content ' . 'database at this time.</p>');
}
$sql = "SELECT \"name\", \"description\" FROM mysql_temp_1.widget WHERE widgetid = $widgetID";
$rs = mysql_query($this->db, $sql);
if (! mysql_num_rows($rs))
{
throw new Exception("An error occured selecting from the database.");
}
$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($this->db, $sql);
if (! is_resource($rs))
{
throw new Exception('An error occured updating the database.');
}
//Your done with the database. Close the connection handle.
mysql_close($this->db);
}
}
?>
chad
|