Jump to content

WithTheBeat

New Members
  • Posts

    1
  • Joined

  • Last visited

    Never

Everything posted by WithTheBeat

  1. 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.
×
×
  • 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.