Jump to content

Wondering if this class is too overboard for 'practicalness'


WithTheBeat

Recommended Posts

Hi there, I wanted to program a SQL class to make my code a bit cleaner. This is it below:

 

<?php

class sql{
var $query;
var $result;

var $main_cat_build;

function sql($INFO){
$c = mysql_connect("$INFO[sql_host]","$INFO[sql_user]","$INFO[sql_pass]") or die("Could not connect:<b> ".mysql_error()."</b>");
mysql_select_db("$INFO[sql_db]", $c) or die("Could not select given database:<b> ".mysql_error()."</b>");

$this->main_cat_build = array(
"action" => "select",
"enable_order" => "1",
"select" => "*",
"from" => "categories",
"where" => "active",
"where_value" => "1",
"order_by" => "`order`",
"order_suffix" => "ASC");

}

//insert into users(username, password, email) VALUES($u,$p,$e)

function fetch_array(){
return mysql_fetch_array($this->result);
}

function build_query($var){

if($var[action] == "insert"){
$this->query = "insert into $var[table]($var[fields]) VALUES($var[values])";
}

if($var[action] == "select"){
if(!$var[operator]){
$var[operator] = "=";
}
if(!$var[disable_where]){
$w = " where ";
}else{
$w = "";
}
$this->query = "select $var[select] from $var[from]$w";
if(!$var[disable_where]){
$this->query.= "$var[where] $var[operator] $var[where_value]";
}
if($var[enable_order]){
$this->query.= " order by $var[order_by] $var[order_suffix]";
}
}// end if(select)
}// end build_query

function exec_built(){// execute built query
$this->result = mysql_query("$this->query") or die(mysql_error());
// then probably empty $this->query..
}

function simple_exec($q){
print"$q";// change this to the query operation once tested
}

}

php?>

 

as you can see in my contructor function I can pre-store arrays to bring up certain query results, and I hope i can build up a useful library.

 

I have a table called testing which has the fields 'id, username, email, password' and just because of right now there's no real content (this was my first step in this little project), and this is how the whole process looks like..

$info = array(
"action" => "select",
"disable_where" => "1",
"select" => "*",
"from" => "testing");

$sql->build_query($info);
$sql->exec_built();
$user = $sql->fetch_array();

 

There's little notes and stuff in my sql class, and it's just the beginning, but I realized how much tinkering it took (for me at least..). I realize there should be quite a few more processes for it to be a universally useful class but I'll add it as I go.

 

So, as far is it looks, is it worth my time and code to keep this going?

Is it more practical than just straight up using 'mysql_query'?

Or if anyone has tips or sees that my code is kinda lame and could be improved let me know.. this is the first time I ever put any of my code out in the open like this so I'm new to how this wooorks. Thank you.

 

Link to comment
Share on other sites

Is it more practical than just straight up using 'mysql_query'?

 

Sorry if this sounds harsh but, I don't think so. In fact I don't really see any benefit at all. Plus it is riddled with syntactically incorrect code that will generate php notices if error reporting is turned up high enough (you are using undefined constants within your array indexes).

 

Your insert statement also won't work with strings.

 

It pretty rare that these database classes people write are of any benefit.

Link to comment
Share on other sites

I have a small database class that just allows me to put together insert/update queries more quickly.

Yea, that's pretty much all that mine does as well. But it only makes things a little easier, not a big deal. The only other advantage that my MySQL wrapper gives is that it allows me to handle MySQL connections more effectively and easily; this is useful when using PHP as the scripting language on SHELL scripts for socket-servers.

Link to comment
Share on other sites

  • 5 weeks later...

In my opinion, you should strive for database classes as an abstraction to the actual connectivity dependencies. For instance, mySQL uses different syntax that Oracle. So you're database abstraction layer would look something like

 

$db = new MySQLLayer(); // or new Orcale // etc all polymorphic of each other
$recordSet = $db->executeStatment($statement);

 

so if you're database change all you have to do if factory in a new Abstraction Layer 1 place in your code! and all the API calls stay the same. Java's JDB is great with this. You might want to look into it a bit. It will blow your mind once you understand it.

http://java.sun.com/docs/books/tutorial/jdbc/basics/retrieving.html

 

Link to comment
Share on other sites

In my opinion, you should strive for database classes as an abstraction to the actual connectivity dependencies. For instance, mySQL uses different syntax that Oracle. So you're database abstraction layer would look something like

$db = new MySQLLayer(); // or new Orcale // etc all polymorphic of each other
$recordSet = $db->executeStatment($statement);

 

While I understand what your saying, your post doesn't really make allot of sense considering that Oracle won't (under circumstance) accept the same $statement as that of mysql.

Link to comment
Share on other sites

I'm not a backend developer, just a good developer. I can't tell you the internals of the JDBC but the idea still remains that it's polymorphic and independent of the database and database specific syntax. Sorry I can't elaborate any further how

Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
Connection conn = DriverManager.getConnection("jdbc:derby:COFFEES");
PreparedStatement updateSales = con.prepareStatement(
        "UPDATE COFFEES SET SALES = ? WHERE COF_NAME LIKE ? ");
updateSales.setInt(1, 75); 
updateSales.setString(2, "Colombian"); 
updateSales.executeUpdate():

runs. Perhaps it deals with the database drivers that JDBC uses as a proxy, but I have no clue about the java internals, jut the theory.

Link to comment
Share on other sites

I'm not a backend developer, just a good developer. I can't tell you the internals of the JDBC but the idea still remains that it's polymorphic and independent of the database and database specific syntax. Sorry I can't elaborate any further how

Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
Connection conn = DriverManager.getConnection("jdbc:derby:COFFEES");
PreparedStatement updateSales = con.prepareStatement(
        "UPDATE COFFEES SET SALES = ? WHERE COF_NAME LIKE ? ");
updateSales.setInt(1, 75); 
updateSales.setString(2, "Colombian"); 
updateSales.executeUpdate():

runs. Perhaps it deals with the database drivers that JDBC uses as a proxy, but I have no clue about the java internals, jut the theory.

 

As I said, I am aware of what you where trying to get at, your post just wasn't overly clear. I use Zend_Db for much the same thing.

Link to comment
Share on other sites

I'm not a backend developer, just a good developer. I can't tell you the internals of the JDBC but the idea still remains that it's polymorphic and independent of the database and database specific syntax. Sorry I can't elaborate any further how

Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
Connection conn = DriverManager.getConnection("jdbc:derby:COFFEES");
PreparedStatement updateSales = con.prepareStatement(
        "UPDATE COFFEES SET SALES = ? WHERE COF_NAME LIKE ? ");
updateSales.setInt(1, 75); 
updateSales.setString(2, "Colombian"); 
updateSales.executeUpdate():

runs. Perhaps it deals with the database drivers that JDBC uses as a proxy, but I have no clue about the java internals, jut the theory.

 

To me, that looks a lot like PDO.

http://www.php.net/pdo

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.