PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 19th, 2012, 07:47 AM
Nullified's Avatar
Nullified Nullified is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 159 Nullified User rank is Sergeant Major (2000 - 5000 Reputation Level)Nullified User rank is Sergeant Major (2000 - 5000 Reputation Level)Nullified User rank is Sergeant Major (2000 - 5000 Reputation Level)Nullified User rank is Sergeant Major (2000 - 5000 Reputation Level)Nullified User rank is Sergeant Major (2000 - 5000 Reputation Level)Nullified User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 19 h 23 m 37 sec
Reputation Power: 30
Lightbulb 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->connectionshow_error('servercon'''$username$password$database$server$port);
        
$this->database mysql_select_db($database$this->connection);
        if (!
$this->databaseshow_error('dbcon'''$username$password$database$server$port);
        
$_SESSION['database'] = true;
    }
    function 
query($sql$connection) {
        
$result mysql_query($sql);
        if (!
$resultshow_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_errorshow_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 (!
$resultshow_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.

Reply With Quote
  #2  
Old November 19th, 2012, 08:01 AM
ptr2void ptr2void is offline
I haz teh codez!
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Dec 2003
Posts: 2,511 ptr2void User rank is General 19th Grade (Above 100000 Reputation Level)ptr2void User rank is General 19th Grade (Above 100000 Reputation Level)ptr2void User rank is General 19th Grade (Above 100000 Reputation Level)ptr2void User rank is General 19th Grade (Above 100000 Reputation Level)ptr2void User rank is General 19th Grade (Above 100000 Reputation Level)ptr2void User rank is General 19th Grade (Above 100000 Reputation Level)ptr2void User rank is General 19th Grade (Above 100000 Reputation Level)ptr2void User rank is General 19th Grade (Above 100000 Reputation Level)ptr2void User rank is General 19th Grade (Above 100000 Reputation Level)ptr2void User rank is General 19th Grade (Above 100000 Reputation Level)ptr2void User rank is General 19th Grade (Above 100000 Reputation Level)ptr2void User rank is General 19th Grade (Above 100000 Reputation Level)ptr2void User rank is General 19th Grade (Above 100000 Reputation Level)ptr2void User rank is General 19th Grade (Above 100000 Reputation Level)ptr2void User rank is General 19th Grade (Above 100000 Reputation Level)ptr2void User rank is General 19th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 11 m 12 sec
Reputation Power: 2274
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!

Reply With Quote
  #3  
Old November 19th, 2012, 08:19 AM
Nullified's Avatar
Nullified Nullified is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 159 Nullified User rank is Sergeant Major (2000 - 5000 Reputation Level)Nullified User rank is Sergeant Major (2000 - 5000 Reputation Level)Nullified User rank is Sergeant Major (2000 - 5000 Reputation Level)Nullified User rank is Sergeant Major (2000 - 5000 Reputation Level)Nullified User rank is Sergeant Major (2000 - 5000 Reputation Level)Nullified User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 19 h 23 m 37 sec
Reputation Power: 30
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.

Reply With Quote
  #4  
Old November 19th, 2012, 12:12 PM
Jacques1's Avatar
Jacques1 Jacques1 is online now
pollyanna
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 2012
Location: Germany
Posts: 2,033 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 6 Days 20 h 59 m 13 sec
Reputation Power: 812
Hi,

you cannot access the static property $connection with
Code:
$this->connection

Turn on your error reporting and you'll see two big warnings.

You either have to make it a normal instance property or use
Code:
self::$connection

Reply With Quote
  #5  
Old November 19th, 2012, 02:21 PM
Nullified's Avatar
Nullified Nullified is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 159 Nullified User rank is Sergeant Major (2000 - 5000 Reputation Level)Nullified User rank is Sergeant Major (2000 - 5000 Reputation Level)Nullified User rank is Sergeant Major (2000 - 5000 Reputation Level)Nullified User rank is Sergeant Major (2000 - 5000 Reputation Level)Nullified User rank is Sergeant Major (2000 - 5000 Reputation Level)Nullified User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 19 h 23 m 37 sec
Reputation Power: 30
I have tried using public and non public var, static and global. none make a difference.

Reply With Quote
  #6  
Old November 19th, 2012, 06:30 PM
E-Oreo's Avatar
E-Oreo E-Oreo is offline
Lost in code
Dev Shed God 7th Plane (8000 - 8499 posts)
 
Join Date: Dec 2004
Posts: 8,057 E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
Time spent in forums: 2 Months 1 Day 5 h 51 m 35 sec
Reputation Power: 7104
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.
__________________
PHP FAQ
How to program a basic, secure login system using PHP
Connect with me on LinkedIn


Quote:
Originally Posted by Spad
Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > PHP5 - Switching from mysql to mysqli

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap