Robert Saylor
asked on
mysqli
I am converting a 10 year old program that uses "mysql_connect" and I want to use mysqli procedural without having to re-write the entire program. The program has a DB class but when I try to use mysqli none of my queries appear to work.
The commented code is mysqli that does not appear to work.
here is my db.php file:
The commented code is mysqli that does not appear to work.
here is my db.php file:
<?php
//Create mysql databse transaction class
class db {
var $con; //Connection Identifer
var $host; //Host name localhost
var $username; // database usernmae
var $password; // database password
var $dbname; // database name
var $dbtype; // database type now I have created only support mysql functions.
var $result;
function db($host,$username,$password,$dbname,$dbtype){ //construct db class and initilaize primary variables.
$this->host = $host;
$this->username = $username;
$this->password = $password;
$this->dbname = $dbname;
$this->dbtype = $dbtype;
}
function dbConnect(){ // mysql normal connect
if($this->dbtype=="mysql"){
$this->con=mysql_connect($this->host, $this->username, $this->password) or die("Connection Error" . mysql_error());
mysql_select_db($this->dbname);
//$this->con=mysqli_connect($this->host, $this->username, $this->password) or die("Connection Error" . mysqli_error());
//mysqli_select_db($this->dbname);
return $this->con;
}
}
function Query($sql){ //execute query and return output
if($this->dbtype=="mysql"){
$this->result =mysql_query($sql) or die("Mysql query error - " . mysql_error() . "<!--[$sql]-->");
//$this->result =mysqli_query($sql) or die("Mysql query error - " . mysqli_error() . "$sql");
return $this->result;
}
}
function GetObject($result){ //fetch Object type query result
if($this->dbtype=="mysql"){
return mysql_fetch_object($result);
//return $mysqli_fetch_object($result);
}
}
function GetRow($result){ //fetch Row type query result
if($this->dbtype=="mysql"){
return mysql_fetch_row($result);
//return $mysqli_fetch_row($result);
}
}
function GetArray($result){
if($this->dbtype=="mysql"){ //fetch Array type query result
return mysql_fetch_array($result);
//return $mysqli_fetch_array($result);
}
}
function Execute($sql){ //Execuat mysql query for non return result queries ie. Update, Delete and Insert
if($this->dbtype=="mysql"){
mysql_query($sql) or die("Mysql query error - " . mysql_error() . "<!--[$sql]-->");
//mysqli_query($sql) or die("Mysql query error - " . mysqli_error() . "<!--[$sql]-->");
}
}
function LastInsertID(){ //return mysql last return id
if($this->dbtype=="mysql"){
return mysql_insert_id();
//return mysqli_insert_id();
}
}
function AffectedRows(){ //return mysql last affected rows
if($this->dbtype=="mysql"){
return mysql_affected_rows($this->con);
//return mysqli_affected_rows($this->con);
}
}
function NumRows($result){ // return total number of fetch result
if($this->dbtype=="mysql"){
return mysql_num_rows($result);
//return mysqli_num_rows($result);
}
}
function MysqlClose(){ // mysql close for the end of the mysql connection but it should need for mysql normat connect
if($this->dbtype=="mysql"){
mysql_close($this->con);
//mysqli_close($this->con);
}
}
}
?>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks will re-write this to OO in the near future before we put PHP 7 on the servers. Got a little time left but not much. I agree with the OO. This is a old program I inherited.
Open in new window