Jump to content

Trying to convert PHP PostgreSQL functions to MySQL with no Luck!!


samoi

Recommended Posts

Hello guys,

I have purchased a new book for PHP called Professional PHP 6. But the bad news is that the whole book was written for PostgreSQL NOT for MYSQL which I'm familiar with!

 

I have this code, I have tried to do so many things to get it working! but nothing seems to have it working!

Long story short, I have failed to convert the code to work with MySQL!

 

Here is my code, in case some one will offer a help, or a reference to go to if some similar case comes up on my way.

<?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 code assumes the DB is called "parts" 
		$this->hDB = pg_connect('dbname=parts user=postgres'); 
		if(! is_resource($this->hDB)) {
			throw new Exception("Unable to connect to the database.");
		}
		$sql = "SELECT name, description FROM widget WHERE widgetid = $widgetID";
		$rs = pg_query($this->hDB, $sql); 
		if(! is_resource($rs)) {
			throw new Exception("An error occurred selecting from the database.");
		}
		if(! pg_num_rows($rs)) {
			throw new Exception("The specified widget does not exist!");
		}
		$data = pg_fetch_array($rs); 
		$this->id = $widgetID;
	}

	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) {
			$sql = "UPDATE widget SET "; 
			$sql .= "name = " .  pg_escape_string($this->name) . ", "; 
			$sql .= "description = " . pg_escape_string($this->description) . ""; 
			$sql .= "WHERE widgetID = " . $this->id;
			$rs = pg_query($this->hDB, $sql);
	}
pg_close($this->hDB);
}
}
?>

 

 

FYI: I have tried mysql_pconnect ! results => failure!

I have tried replacing the prefix "pg" to "mysql" or "mysqli"! results => failure!

I have tried switching parameters, say in pg_query($resource, $query) TO mysql_query($query, $resource)  results => failure too!

 

 

 

 

Thank you in advance!

Link to comment
Share on other sites

You'll need to change both the functions and the arguments.  They all have much the same names but they are called slightly differently.  The first thing you need to do is get the connection working - don't bother with any of the rest until that's done.  It will need to use the username you have set up for MySQL.

 

Luckily the SQL will be much the same .. you may have to change some of the table definitions though (the "CREATE TABLE"s).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.