 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
11-06-2007, 11:57 PM
|
#1 (permalink)
|
|
The Acquainted
Join Date: Sep 2007
Location: Arizona
Posts: 114
Thanks: 10
|
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
|
|
|
11-07-2007, 01:47 AM
|
#2 (permalink)
|
|
The Addict
Join Date: Nov 2007
Posts: 282
Thanks: 61
|
Post your code and post your errors.
|
|
|
|
11-07-2007, 04:40 AM
|
#3 (permalink)
|
|
The Acquainted
Join Date: Sep 2007
Location: Arizona
Posts: 114
Thanks: 10
|
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
|
|
|
11-07-2007, 06:19 AM
|
#4 (permalink)
|
|
The Contributor
Join Date: Sep 2007
Posts: 31
Thanks: 0
|
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_HOST, DB_USER, DB_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.
|
|
|
|
|
The Following User Says Thank You to daz For This Useful Post:
|
|
11-07-2007, 07:30 AM
|
#5 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
Actually, I wouldn't use the "define" structure.
Just use regular variables.
|
|
|
|
11-07-2007, 09:33 AM
|
#6 (permalink)
|
|
Super Moderator
Join Date: Sep 2007
Posts: 165
Thanks: 0
|
Quote:
Originally Posted by Tanax
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!
|
|
|
|
11-07-2007, 12:31 PM
|
#7 (permalink)
|
|
The Reckoner
Join Date: Sep 2007
Posts: 437
Thanks: 22
|
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.
|
|
|
|
11-07-2007, 12:41 PM
|
#8 (permalink)
|
|
Super Moderator
Join Date: Sep 2007
Posts: 165
Thanks: 0
|
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;
|
|
|
|
11-08-2007, 09:10 PM
|
#9 (permalink)
|
|
The Prestige
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
|
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)
|
|
|
|
06-27-2008, 06:52 AM
|
#10 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Quote:
Originally Posted by bluesaga
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
|
|
|
|
06-30-2008, 01:56 PM
|
#11 (permalink)
|
|
The Wanderer
Join Date: Apr 2008
Location: Cloud 9
Posts: 19
Thanks: 0
|
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?
|
|
|
|
|
The Following User Says Thank You to Jenski For This Useful Post:
|
|
06-30-2008, 03:32 PM
|
#12 (permalink)
|
|
The Contributor
Join Date: Jan 2008
Location: England, UK
Posts: 83
Thanks: 3
|
Quote:
Originally Posted by bluesaga
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.
|
|
|
|
07-14-2009, 03:49 PM
|
#13 (permalink)
|
|
The Contributor
Join Date: Jul 2009
Posts: 80
Thanks: 13
|
What if a web host is not allowing mysqli as my server has it turned off. can i use mysql instead?
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
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
|
|
|
|