The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
PHP5 - Switching from mysql to mysqli
Discuss Switching from mysql to mysqli in the PHP Development forum on Dev Shed. Switching from mysql to mysqli PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 19th, 2012, 07:47 AM
|
 |
Contributing User
|
|
|
|
PHP5 - Switching from mysql to mysqli
My old class utilizing mysql functions works properly:
PHP Code:
class Database {
var $connection = null;
var $database = null;
function connect($username, $password, $database, $server = 'localhost', $port = 3306, $persistent = false) {
$this->connection = empty($persistent) ? mysql_connect("$server:$port", $username, $password) : mysql_pconnect("$server:$port", $username, $password);
if (!$this->connection) show_error('servercon', '', $username, $password, $database, $server, $port);
$this->database = mysql_select_db($database, $this->connection);
if (!$this->database) show_error('dbcon', '', $username, $password, $database, $server, $port);
$_SESSION['database'] = true;
}
function query($sql, $connection) {
$result = mysql_query($sql);
if (!$result) show_error('query', $sql, $username, $password, $database, $server, $port);
return $result;
@mysql_free_result($result);
}
function insert($sql) {
$this->query($sql, $this->connection);
}
function update($sql) {
$this->query($sql, $this->connection);
}
function delete($sql) {
$this->query($sql, $this->connection);
}
function fetch_array($sql, $type = MYSQL_ASSOC) {
$result = $this->query($sql, $this->connection);
while($row = mysql_fetch_array($result, $type)) {
is_array($array) ? array_push($array, $row) : $array = array($row);
}
return $array;
@mysql_free_result($array);
}
}
However, I cannot get mysqli to work properly. I have tried so many variations of code.
PHP Code:
class Database {
public static $connection;
public function connect($username, $password, $database, $server = 'localhost', $port = 3306) {
$this->connection = mysqli_connect($server, $username, $password, $database);
if ($this->connection->connect_error) show_error('dbcon', '', $username, $password, $database, $server, $port);
$_SESSION['database'] = true;
}
public function disconnect() {
mysqli_close($this->connection);
$this->connection = null;
}
public function query($sql, $connection) {
$result = mysqli_query($connection, $sql);
if (!$result) show_error('query', $sql, $username, $password, $database, $server, $port);
return $result;
@mysqli_free_result($result);
}
public function insert($sql) {$this->query($sql, $this->connection);}
public function update($sql) {$this->query($sql, $this->connection);}
public function delete($sql) {$this->query($sql, $this->connection);}
public function fetch_array($sql, $type = MYSQL_ASSOC) {
$result = $this->query($sql, $this->connection);
while($row = mysqli_fetch_array($result, $type)) {
is_array($array) ? array_push($array, $row) : $array = array($row);
}
return $array;
@mysqli_free_result($array);
}
}
It seems that the connection set in the function connect is null when the query functions are called.
|

November 19th, 2012, 08:01 AM
|
|
|
|
I would suggest reading the parameters from a config file. Then you can check the $this->connection variable before issuing a query, and if it's null, then use $this->connect.
It's difficult to help with the specific issue without seeing how exactly you're using the database class otherwise.
EDIT: Also, if query is a member variable of the class, why are you forcing a connection to be passed in, instead of using the one you have in the class?
__________________
I ♥ ManiacDan & requinix
This is a sig, and not necessarily a comment on the OP:
Please don't be a help vampire!
|

November 19th, 2012, 08:19 AM
|
 |
Contributing User
|
|
|
|
|
I am pulling the connection parameters from a config file. I am checking $this->connection with "if (is_null($this->connection)) die("No DB Connection");" but after removing $connection from the query function parameters and using $this->connection it still does not work. It's working fine on my mysql php v4 code.
Last edited by Nullified : November 19th, 2012 at 08:21 AM.
|

November 19th, 2012, 12:12 PM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
Hi,
you cannot access the static property $connection with
Turn on your error reporting and you'll see two big warnings.
You either have to make it a normal instance property or use
|

November 19th, 2012, 02:21 PM
|
 |
Contributing User
|
|
|
|
|
I have tried using public and non public var, static and global. none make a difference.
|

November 19th, 2012, 06:30 PM
|
 |
Lost in code
|
|
|
|
Quote: | I am checking $this->connection with "if (is_null($this->connection)) die("No DB Connection");" |
That line doesn't appear anywhere in the code you posted. What does your current code look like?
Quote: | I have tried using public and non public var, static and global. none make a difference. |
Remove the word static. public/private/var makes no difference in this case. global is irrelevant to class variables.
Change your connect method into your constructor. That way you cannot possibly create a new database object with setting up a connection. My guess is you are forgetting to call your connect method after constructing the object.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|