![]() |
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 |
Post your code and post your errors.
|
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 |
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:
PHP Code:
|
Actually, I wouldn't use the "define" structure.
Just use regular variables. |
Quote:
|
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 |
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); |
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.
|
Quote:
|
Hi Andrew
In this function: Code:
public function query($szQuery) { |
Quote:
|
What if a web host is not allowing mysqli as my server has it turned off. can i use mysql instead?
|
| All times are GMT. The time now is 04:35 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0