Jump to content

Recommended Posts

Hi all, I inherited code from a previous developer which was specific to PHP 4.0 and we have upgraded, finally to 5.0+ and I need to know what needs to be changed the following code (if anything) to get it up to 5.0 standards.

 

<?php
//uses PHP4
class Database {

var $host;
var $user;
var $pwd;
var $dbName;
var $flash;
var $dbLink;
var $result;
var $resultObj;

function Database($host, $user, $pwd, $dbName, $flash=1){
	$this->host = $host;
	$this->user = $user;
	$this->pwd = $pwd;
	$this->dbName = $dbName;
	$this->flash = $flash;
	$this->connect();
	}

//Connect to MySQL server and select database
function connect() {
	$this->dbLink = @mysql_pconnect($this->host, $this->user, $this->pwd);
	if(!$this->dbLink) {
		$error = 'Couldn\'t connect to the MySQL server.';
		echo $this->flash ? 'error='.urlencode($error) : $error;
		exit();
		}
	if(!mysql_select_db($this->dbName, $this->dbLink)){
		$error = 'Couldn\'t open Database: '.$this->dbName;
		echo $this->flash ? 'error='.urlencode($error) : $error;
		exit();
		}
	return $this->dbLink;
	}

//Execute a SQL query
function query($query) {
	$this->result = mysql_query($query, $this->dbLink);
	if (!$this->result) {
		$error = 'MySQL Error: '.mysql_error();
		echo $this->flash ? 'error='.urlencode($error) : $error;
		exit();
		}
	//store result in new object to emulate mysqli OO interface
	$this->resultObj = new MyResult($this->result);
	return $this->resultObj;
	}

function close(){
	//close MySQL connection
	mysql_close($this->dbLink);
	}
}

class MyResult {

var $theResult;
var $num_rows;

function MyResult(&$r) {
	$this->theResult = $r;
	}

function get_rows() {
	//get total number of records found
	$this->num_rows = mysql_num_rows($this->theResult);
	return $this->num_rows;
	}

//fetch associative array of result (one row at a time)
function fetch_assoc() {
	$newRow = mysql_fetch_assoc($this->theResult);
	return $newRow;
	}
}



?>

that looks ok. One thing is you don't need to use the keyword "var" anymore to define a class variable. but you may want to alter the scope of some of those variables if you wish to take advantage of encapsulation

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.