Author Topic: Database Class :: Feedback Requested  (Read 1133 times)

Offline Waizujin

  • Level 15
  • *
  • Posts: 132
  • Reputation: +1/-0
    • View Profile
Database Class :: Feedback Requested
« on: October 05, 2011, 03:42:19 PM »
I create a database class and I was hoping people could comment on it. Let me know if anything can be improved. :)

It's VERY simple.

Offline BlackScorp

  • Level 15
  • *
  • Posts: 123
  • Reputation: +6/-0
    • View Profile
    • Cruel Online
Re: Database Class :: Feedback Requested
« Reply #1 on: October 05, 2011, 04:18:08 PM »
Well i wouldnt add mysql_connect and select db into constructor, i would add it in a static function. so you can call db::connect(); once in the bootstrap php and you can just execute the mysql query. otherwise you connect on every included page, send some querys, disconnect , then connect again and so on..

instead of Fetch or query methods, i would check what kind of Query you have and give back an result over parameter.
it had some advantages:
a) you dont have to remember when to use which method
b) with parameters what kind of result you want to have i could be more flexible(for example if i want only to get one row, or i want to get mysql_fetch_object)

then , vsprintf is nice but you dont escape your values.. or do i have it before calling the method?

and last but not least you dont throw any errors. if i use the class and i dont get the right result i dont know why, is it because your class has a bug? or because my query has a wrong syntax?

try change it:D

Greeting blackscorp

Offline ajacksified

  • Level 5
  • *
  • Posts: 18
  • Reputation: +0/-0
    • View Profile
    • Olivine Labs
Re: Database Class :: Feedback Requested
« Reply #2 on: October 05, 2011, 04:28:18 PM »
My initial reaction is that you should probably be using mysqli rather than mysql (reference: http://uk3.php.net/manual/en/mysqli.overview.php)

That said, what does your database class provide that the mysqli extension doesn't? You're still passing in raw sql to run. Is the automatic execution / return of objects / result rows what you're after?

(Just asking some questions / giving you feedback; don't take this as negative criticism.)

Offline BlackScorp

  • Level 15
  • *
  • Posts: 123
  • Reputation: +6/-0
    • View Profile
    • Cruel Online
Re: Database Class :: Feedback Requested
« Reply #3 on: October 06, 2011, 12:54:36 AM »
instea of mysql and mysqli, i would implement PDO but thats another points

Offline Barrikor

  • Level 21
  • *
  • Posts: 247
  • Reputation: +3/-0
    • View Profile
Re: Database Class :: Feedback Requested
« Reply #4 on: October 12, 2011, 05:27:04 PM »
It's a good way to start the class, you'll want to add more functions to make the class do more later.

Like the others have pointed out:
- the mysqli set of functions is the newer+better way to use mysql now
- You could filter or escape the vars in your query() function


Personally I'd take connect and disconnect out of __construct() and __destruct() and give them their own functions, but that depends on your style and how you're gonna use the class.
Projects: Pith Framework (at 0.5), CactusGUI (at 0.3)

Offline BlackScorp

  • Level 15
  • *
  • Posts: 123
  • Reputation: +6/-0
    • View Profile
    • Cruel Online
Re: Database Class :: Feedback Requested
« Reply #5 on: October 13, 2011, 02:46:48 AM »
"the mysqli set of functions is the newer+better way to use mysql now"

why does everyone says that? MySQLi is a complete another Database, so before you use mysqli_* functions you have to install the MySQLi Database on your webspace. mysqli_* functions are the same like mysql_* the different between mysqli und mysqli is , that youre able to use mysqli as classes. youre able to use somethink like $sql = new mysqli($connectArray); and after it $sql->query("SELECT * Bla"); that would be the newer and better way, but if you use mysqli_connect or mysqli_select_db and such that would not be the better way.

at the end, the Best way, which is used by most Frameworks is to use PDO or ORM which use the PDO.. but mysqli_* is not better than mysql_*

Offline ajacksified

  • Level 5
  • *
  • Posts: 18
  • Reputation: +0/-0
    • View Profile
    • Olivine Labs
Re: Database Class :: Feedback Requested
« Reply #6 on: October 13, 2011, 06:26:20 PM »
"the mysqli set of functions is the newer+better way to use mysql now"

why does everyone says that? MySQLi is a complete another Database, so before you use mysqli_* functions you have to install the MySQLi Database on your webspace. mysqli_* functions are the same like mysql_* the different between mysqli und mysqli is , that youre able to use mysqli as classes. youre able to use somethink like $sql = new mysqli($connectArray); and after it $sql->query("SELECT * Bla"); that would be the newer and better way, but if you use mysqli_connect or mysqli_select_db and such that would not be the better way.

at the end, the Best way, which is used by most Frameworks is to use PDO or ORM which use the PDO.. but mysqli_* is not better than mysql_*



I assume that from your assumption that mysqli is another database that you aren't familiar with mysql connections with PHP. I'd look at the mysqli overview page here to get an idea: http://php.net/manual/en/mysqli.overview.php

Some people use PDO, sure. But your assumption that mysql is just as good is false.
« Last Edit: October 13, 2011, 06:40:46 PM by ajacksified »

Offline BlackScorp

  • Level 15
  • *
  • Posts: 123
  • Reputation: +6/-0
    • View Profile
    • Cruel Online
Re: Database Class :: Feedback Requested
« Reply #7 on: October 14, 2011, 03:34:17 AM »

Quote
MySQLi is not "another database"; it's a newer library for php to deal with MySql
oh that is my Fault. last time i used MySQL with Xampp there was an Option for MySQL it called MySQL light. It was Basicly a Textfile Database. Since iam using only PDO iam not really up to date.

Quote
You can structure parameterized queries in a much more secure way instead of worrying so much about SQL injection attacks.
Only if you use Prepared Statments, else you have to Escape your Queries Anyways

Quote
It follows OOP paradigms
Only if you use OOP way of mysqli, if you use it Procedural so it is kinda same like MySQL

Quote
Transaction support
not everytime needed.

Quote
Multiple query support
because of Prepared Statements

Quote
If you are using MySQL versions 4.1.3 or later it is strongly recommended that you use the mysqli extension instead. from the php docs
Actually MySQL version is 5.5.16, if your server isnt up to date then i would agree.

Quote
But your assumption that mysql is just as good is false.
i told that MySQLi is only better if you using the OOP Way of MySQLi. if you use procedural way it is kinda same like MySQL.

i agree that MySQli has more features to MySQL but only to telling them to use mysqli instead of mysql, they would just replace their function calls in the code and that would not be the better way... that is what iam tryin to say:D (damn english)

Best regards BlackScorp

Offline hiigara

  • Level 12
  • *
  • Posts: 85
  • Reputation: +0/-0
    • View Profile
Re: Database Class :: Feedback Requested
« Reply #8 on: October 14, 2011, 06:15:30 AM »

  • Transaction support



You don't need to use mysqli to use transactions.

What's wrong with doing:
mysql_query ("start transaction");   ?
« Last Edit: October 14, 2011, 06:18:05 AM by hiigara »

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: Database Class :: Feedback Requested
« Reply #9 on: October 14, 2011, 07:07:02 AM »
Why is it said that MySQLi is better than MySQL? Anyone knows some details?
I have not noticed people rewriting their code to the "i", so I'm a bit sceptical...

Offline BlackScorp

  • Level 15
  • *
  • Posts: 123
  • Reputation: +6/-0
    • View Profile
    • Cruel Online
Re: Database Class :: Feedback Requested
« Reply #10 on: October 14, 2011, 07:42:35 AM »
Why is it said that MySQLi is better than MySQL? Anyone knows some details?
I have not noticed people rewriting their code to the "i", so I'm a bit sceptical...

its because of the functions for Prepared Statements and Multiple Queries if you just just replace mysql_* to mysqli_* there is nothink better then.

Offline ajacksified

  • Level 5
  • *
  • Posts: 18
  • Reputation: +0/-0
    • View Profile
    • Olivine Labs
Re: Database Class :: Feedback Requested
« Reply #11 on: October 14, 2011, 05:07:43 PM »
Quote
You can structure parameterized queries in a much more secure way instead of worrying so much about SQL injection attacks.
Only if you use Prepared Statments, else you have to Escape your Queries Anyways

Why would you not use prepared statements?

Quote
Quote
If you are using MySQL versions 4.1.3 or later it is strongly recommended that you use the mysqli extension instead. from the php docs
Actually MySQL version is 5.5.16, if your server isnt up to date then i would agree.

It says if you're using 4.1.3 or later, you should be using mysqli. So, yeah, with 5.5.16, you should be using mysqli, not mysql.


Quote
i agree that MySQli has more features to MySQL but only to telling them to use mysqli instead of mysql, they would just replace their function calls in the code and that would not be the better way... that is what iam tryin to say

It's not just about what has more features, it's also about what's currently being worked on, and what's generally accepted as good practice. (From the official php docs, that's mysqli.) It's about cleaner code and better security (read: prepared statements).

Unless you have a tangible reason to use mysql, you shouldn't- and chances are really high that you don't. I'm suggesting using modern practice rather than sticking with "well, that's what I did in 2005, so I guess it's alright."

Offline BlackScorp

  • Level 15
  • *
  • Posts: 123
  • Reputation: +6/-0
    • View Profile
    • Cruel Online
Re: Database Class :: Feedback Requested
« Reply #12 on: October 15, 2011, 03:43:13 AM »
thats why i started to use PDO :D so i dont have to care about mysql or mysqli or maybe some days there will be a new lib mysql super or somethink like that:D PDO is up to date and iam happy with it:D

Offline codestryke

  • Administrator
  • Level 33
  • *****
  • Posts: 589
  • Reputation: +22/-0
    • View Profile
    • eXtremeCast Games
Re: Database Class :: Feedback Requested
« Reply #13 on: October 15, 2011, 09:50:39 AM »
Why would you not use prepared statements?
For every query you are going to the database twice (once to prepare the sql and the second to execute the query). Prepared statements are expensive to run because of the additional trip to the server.

Creating online addictions, one game at a time:

Offline hiigara

  • Level 12
  • *
  • Posts: 85
  • Reputation: +0/-0
    • View Profile
Re: Database Class :: Feedback Requested
« Reply #14 on: October 15, 2011, 12:09:35 PM »
If you are a beginner learning PHP for the first time, then I would recommend learning mysqli instead of mysql.
But even in this case, most online tutorials use plain mysql.
People who already use plain mysql will not bother to migrate to mysqli, because if there is a bottleneck, it will not be solved by using mysqli. The gains are very small.

Offline BlackScorp

  • Level 15
  • *
  • Posts: 123
  • Reputation: +6/-0
    • View Profile
    • Cruel Online
Re: Database Class :: Feedback Requested
« Reply #15 on: October 17, 2011, 03:40:33 AM »
Why would you not use prepared statements?
For every query you are going to the database twice (once to prepare the sql and the second to execute the query). Prepared statements are expensive to run because of the additional trip to the server.



i think if your page make about 5 quries each call you dont even need prepared statements, i think they are made for sites who make 1000 Quries:D for example if some one using like this

$sql1 = "SELECT post,user_id... FROM forum WHERE topic_id = 12345"

foreach($row = mysql_fetch_assoc($sql1)){
$sql2 = "SELECT username FROM users WHERE id = ".$row->user_id
}

then ppl prepare those statements and sending them instead of using joins

Offline Waizujin

  • Level 15
  • *
  • Posts: 132
  • Reputation: +1/-0
    • View Profile
Re: Database Class :: Feedback Requested
« Reply #16 on: December 29, 2011, 01:31:54 PM »
It's been a while since I posted this, but I wanted to let you guys know that I eventually decided to switch to using PDO and am very happy with it thus far. :)

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal