TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Using a MySQL class (http://www.talkphp.com/general/1399-using-mysql-class.html)

Andrew 11-06-2007 11:57 PM

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

Nor 11-07-2007 01:47 AM

Post your code and post your errors.

Andrew 11-07-2007 04:40 AM

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

daz 11-07-2007 06:19 AM

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.

Tanax 11-07-2007 07:30 AM

Actually, I wouldn't use the "define" structure.
Just use regular variables.

bluesaga 11-07-2007 09:33 AM

Quote:

Originally Posted by Tanax (Post 3726)
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!

Karl 11-07-2007 12:31 PM

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

bluesaga 11-07-2007 12:41 PM

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;


sketchMedia 11-08-2007 09:10 PM

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.

Orc 06-27-2008 06:52 AM

Quote:

Originally Posted by bluesaga (Post 3735)
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

Jenski 06-30-2008 01:56 PM

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?

Ross 06-30-2008 03:32 PM

Quote:

Originally Posted by bluesaga (Post 3735)
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.

russellharrower 07-14-2009 03:49 PM

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