cmgmyr Posted March 15, 2007 Share Posted March 15, 2007 So I'm starting out in OOP and I decided to make my own little MySQL class. Can you please review and let me know if there is anything else i NEED either to make it better or for security. Will this be ok to use for a large site with lots of queries going on at the same time? Thanks mysql.php <?php $host = "localhost"; $username = "root"; $password = "password"; $database = "database"; // Connect to MySQL $connection = mysql_connect($host, $username, $password); // Select desired database $link = mysql_select_db($database, $connection); if (!$link) { die('Fatal Error: Could not connect to MySQL Database' . mysql_error()); } class mysql{ //Query MySQL Database function query($sql){ $result = mysql_query($sql); return $result; } //Fetch array Query MySQL Database function fetch($result) { $rows = mysql_fetch_array($result); return $rows; } //Count the number of rows in query function count_rows($result) { $count = mysql_num_rows($result); return $count; } } ?> test_mysql.php <?php // Include MySQL class require_once 'mysql.php'; // Instantiate MySQL class, connect to MySQL and select database $db = new mysql; $sql = "SELECT * FROM users WHERE type = 'f' ORDER BY name ASC"; // Perform a query selecting five articles $result = $db->query($sql); // Perform a count $count = $db->count_rows($result); echo "$count<br /><br />"; // Display the results while ($row = $db->fetch($result)) { $name = stripslashes($row['name']); echo "$name<br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/42864-my-mysql-classsimple/ Share on other sites More sharing options...
effigy Posted March 15, 2007 Share Posted March 15, 2007 - I would use constants for the connection information. - If you're using MySQL 4.1+, I would extend the mysqli class instead. I posted some of my extension work here. Quote Link to comment https://forums.phpfreaks.com/topic/42864-my-mysql-classsimple/#findComment-208150 Share on other sites More sharing options...
per1os Posted March 15, 2007 Share Posted March 15, 2007 I would even put that in the constructor and pass the data via config. Again I will post my class here for people to view how I did it =) Quote Link to comment https://forums.phpfreaks.com/topic/42864-my-mysql-classsimple/#findComment-208158 Share on other sites More sharing options...
Daniel0 Posted March 15, 2007 Share Posted March 15, 2007 Basically, put all of it in the mysql class. Quote Link to comment https://forums.phpfreaks.com/topic/42864-my-mysql-classsimple/#findComment-208197 Share on other sites More sharing options...
ignace Posted March 18, 2007 Share Posted March 18, 2007 when you are creating a database abstraction class, make sure it will supports more then just one type of database (eg: mysql, mysqli, postgresql, ...) class DB { var $container; var $conn; function DB($container = 'MySQL', $options = '') { $this->container = new $container($options); } function connect($server, $username, $password) { $this->container->connect($server, $username, $password); } } // container MySQL class MySQL extends DB { function connect($server, $username, $password) { $this->conn = mysql_connect($server, $username, $password); } } // container PostGreSQL class PostGreSQL extends DB { function connect($server, $username, $password) { $this->conn = pg_connect(sprintf("host=%s user=%s password=%s", $server, $username, $password)); } } Quote Link to comment https://forums.phpfreaks.com/topic/42864-my-mysql-classsimple/#findComment-210129 Share on other sites More sharing options...
neylitalo Posted March 19, 2007 Share Posted March 19, 2007 when you are creating a database abstraction class, make sure it will supports more then just one type of database (eg: mysql, mysqli, postgresql, ...) Yes, that is a crucial aspect of a database abstraction library, but if you'll notice, cmgmyr was not out to create a database abstraction library. He says "MySQL class", which leads me to believe that he's not going to be using it with a postgres/sqlite/etc. database. Quote Link to comment https://forums.phpfreaks.com/topic/42864-my-mysql-classsimple/#findComment-210269 Share on other sites More sharing options...
cmgmyr Posted March 19, 2007 Author Share Posted March 19, 2007 true, thanks for that though. eventually i might want to make a "framework" where I would need that, but right now I just need MySQL Quote Link to comment https://forums.phpfreaks.com/topic/42864-my-mysql-classsimple/#findComment-210273 Share on other sites More sharing options...
ignace Posted March 20, 2007 Share Posted March 20, 2007 when you are creating a database abstraction class, make sure it will supports more then just one type of database (eg: mysql, mysqli, postgresql, ...) Yes, that is a crucial aspect of a database abstraction library, but if you'll notice, cmgmyr was not out to create a database abstraction library. He says "MySQL class", which leads me to believe that he's not going to be using it with a postgres/sqlite/etc. database. yes, ofcourse but when they want to play with fire, you might aswell give them a lighter.. Quote Link to comment https://forums.phpfreaks.com/topic/42864-my-mysql-classsimple/#findComment-211072 Share on other sites More sharing options...
cmgmyr Posted March 20, 2007 Author Share Posted March 20, 2007 Fire? where? Quote Link to comment https://forums.phpfreaks.com/topic/42864-my-mysql-classsimple/#findComment-211096 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.