Jump to content

need help adjusting 4.0 specific code to 5.0


webguync

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;
	}
}



?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.