TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-06-2007, 11:57 PM   #1 (permalink)
The Acquainted
 
Join Date: Sep 2007
Location: Arizona
Posts: 114
Thanks: 10
Andrew is on a distinguished road
Default Using a MySQL class

Well, I have written out a good chunk of a MySQL class that I would like to use in some of my scripts. I'm just wondering how I implement it?

I included both of my classes (MySQL and Admit) in a config.php file and initiated them using the new statement. However, this doesn't allow me to use the MySQL functions in my other class. So I've messed around with the includes and declarations, trying to put them in the class file, to no avail.

I then tried extending the MySQL class with my other one, but I just get more errors about missing arguments for __construct() (even though they are there).

I'm just curious the CORRECT way to implement MySQL classes, allowing you to use $mysql->query(); for example in another class.

Thanks, and I hope this made sense.
Andrew
Send a message via AIM to Andrew Send a message via MSN to Andrew
Andrew is offline  
Reply With Quote
Old 11-07-2007, 01:47 AM   #2 (permalink)
Nor
The Addict
 
Join Date: Nov 2007
Posts: 282
Thanks: 61
Nor is on a distinguished road
Default

Post your code and post your errors.
Nor is offline  
Reply With Quote
Old 11-07-2007, 04:40 AM   #3 (permalink)
The Acquainted
 
Join Date: Sep 2007
Location: Arizona
Posts: 114
Thanks: 10
Andrew is on a distinguished road
Default

Don't mind the code too much, just wrote it quick to see if it would work, and still haven't commented or anything.

MySQL Class:
http://phpfi.com/274351
Admit Class: (just noticed I had a bracket in my query by my mistake, so removed it, but didn't change anything)
http://phpfi.com/274353
Config File:
http://phpfi.com/274354

Errors when using the 'extend' route:
Warning: Missing argument 1 for MySQL::__construct(), called in C:\xampp\htdocs\AdmitSomething\config.php on line 13 and defined in C:\xampp\htdocs\AdmitSomething\includes\mysql.clas s.php on line 16

Warning: Missing argument 2 for MySQL::__construct(), called in C:\xampp\htdocs\AdmitSomething\config.php on line 13 and defined in C:\xampp\htdocs\AdmitSomething\includes\mysql.clas s.php on line 16

Warning: Missing argument 3 for MySQL::__construct(), called in C:\xampp\htdocs\AdmitSomething\config.php on line 13 and defined in C:\xampp\htdocs\AdmitSomething\includes\mysql.clas s.php on line 16

Warning: Missing argument 4 for MySQL::__construct(), called in C:\xampp\htdocs\AdmitSomething\config.php on line 13 and defined in C:\xampp\htdocs\AdmitSomething\includes\mysql.clas s.php on line 16
Send a message via AIM to Andrew Send a message via MSN to Andrew
Andrew is offline  
Reply With Quote
Old 11-07-2007, 06:19 AM   #4 (permalink)
daz
The Contributor
Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 31
Thanks: 0
daz is on a distinguished road
Default

When you create an instance of your MySQL class, the connect() function should be called in your constructor. As for your database information, it's better to define them in your class. e.g.

PHP Code:
    define("DB_HOST""localhost");
    
define("DB_USER""root");
    
define("DB_PASS"""); 
And in your connect function:

PHP Code:
        public function connectToMySQL() {    
            @
mysql_connect(DB_HOSTDB_USERDB_PASS) OR die("Cannot connect to MySQL server!");    
            
mysql_select_db("login") OR die("Cannot select database!");
        } 
Then just call the connection in your constructor.
daz is offline  
Reply With Quote
The Following User Says Thank You to daz For This Useful Post:
codefreek (06-30-2008)
Old 11-07-2007, 07:30 AM   #5 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Actually, I wouldn't use the "define" structure.
Just use regular variables.
Tanax is offline  
Reply With Quote
Old 11-07-2007, 09:33 AM   #6 (permalink)
Super Moderator
Advanced Programmer 
 
bluesaga's Avatar
 
Join Date: Sep 2007
Posts: 165
Thanks: 0
bluesaga is on a distinguished road
Default

Quote:
Originally Posted by Tanax View Post
Actually, I wouldn't use the "define" structure.
Just use regular variables.
Personally i like using the DEFINE route for DB credentials, makes them seperate easily. And configuration files are always nice to use define's instead of variables, its neater and why not use them when your variable is static anyway!
__________________
Halo 3 Cheats
bluesaga is offline  
Reply With Quote
Old 11-07-2007, 12:31 PM   #7 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

On the subject of "defines", why arn't people using the PHP5 equivalent, that's, const. I see it all the time, people create a Database class (for example) then place the defines that work with it in the global scope, rather than using class to encapsulate this information.

Another advantage of this is that you don't have to be so strict with your const names, i.e.

Member::ACTIVE_FLAG
Job::ACTIVE_FLAG

rather than

MEMBER_ACTIVE_FLAG
JOB_ACTIVE_FLAG
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Karl is offline  
Reply With Quote
Old 11-07-2007, 12:41 PM   #8 (permalink)
Super Moderator
Advanced Programmer 
 
bluesaga's Avatar
 
Join Date: Sep 2007
Posts: 165
Thanks: 0
bluesaga is on a distinguished road
Default

Also, while on the topic of mysql and PHP5 - i would recommend everyone look at www.php.net/mysqli it uses an object orientated approach to mysql.

The Mysqli format of doing this is especially helpful when working with multiple databases as instead of having to give a pointer to every query, you simply use the initialized db as a class ie:

Code:
$db       = new mysqli("localhost", "username", "password", "database", 3006);
$db2      = new mysqli("localhost", "username", "password", "database2", 3006);
$query    = $db->query("SELECT count(*) FROM example");
$query2   = $db2->query("SELECT count(*) FROM example");
echo $query->num_rows . "|" . $query2->num_rows;
__________________
Halo 3 Cheats
bluesaga is offline  
Reply With Quote
Old 11-08-2007, 09:10 PM   #9 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

mysqli is great, it also offers prepared statements, prepared statements provide the ability to create queries that are more secure, have better performance and are easier to code in my opinion.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 06-27-2008, 06:52 AM   #10 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by bluesaga View Post
Also, while on the topic of mysql and PHP5 - i would recommend everyone look at PHP: Mysqli - Manual it uses an object orientated approach to mysql.

The Mysqli format of doing this is especially helpful when working with multiple databases as instead of having to give a pointer to every query, you simply use the initialized db as a class ie:

Code:
$db       = new mysqli("localhost", "username", "password", "database", 3006);
$db2      = new mysqli("localhost", "username", "password", "database2", 3006);
$query    = $db->query("SELECT count(*) FROM example");
$query2   = $db2->query("SELECT count(*) FROM example");
echo $query->num_rows . "|" . $query2->num_rows;
I use that over the standard mysql functions. :P
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 06-30-2008, 01:56 PM   #11 (permalink)
The Wanderer
 
Join Date: Apr 2008
Location: Cloud 9
Posts: 19
Thanks: 0
Jenski is on a distinguished road
Default

Hi Andrew

In this function:
Code:
public function query($szQuery) {
        $this->m_query = mysql_query($szQuery);
        if (!$this->m_query) {
            return 'There was an error while trying to process the query.';
        } else {
            return $this->m_query;
        }
    }
You use a class variable called m_query which isn't defined in the scope of the class only the function. I don't know if that is relevant?
Jenski is offline  
Reply With Quote
The Following User Says Thank You to Jenski For This Useful Post:
codefreek (06-30-2008)
Old 06-30-2008, 03:32 PM   #12 (permalink)
The Contributor
 
Ross's Avatar
 
Join Date: Jan 2008
Location: England, UK
Posts: 83
Thanks: 3
Ross is on a distinguished road
Default

Quote:
Originally Posted by bluesaga View Post
Also, while on the topic of mysql and PHP5 - i would recommend everyone look at PHP: Mysqli - Manual it uses an object orientated approach to mysql.

The Mysqli format of doing this is especially helpful when working with multiple databases as instead of having to give a pointer to every query, you simply use the initialized db as a class ie:

Code:
$db       = new mysqli("localhost", "username", "password", "database", 3006);
$db2      = new mysqli("localhost", "username", "password", "database2", 3006);
$query    = $db->query("SELECT count(*) FROM example");
$query2   = $db2->query("SELECT count(*) FROM example");
echo $query->num_rows . "|" . $query2->num_rows;
I agree - it's very easy to work with and I generally prefer the object-oriented approach. However if you already have a multi-platform database class (e.g. a DAL) it's quite hard to implement as it uses seperate classes for results and the actual connection.
Ross is offline  
Reply With Quote
Old 07-14-2009, 03:49 PM   #13 (permalink)
The Contributor
 
russellharrower's Avatar
 
Join Date: Jul 2009
Posts: 80
Thanks: 13
russellharrower is on a distinguished road
Default

What if a web host is not allowing mysqli as my server has it turned off. can i use mysql instead?
russellharrower is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 03:14 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design