473,387 Members | 1,476 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

simplify insert-update-delete from database

hello

I'm looking for some functions or objects allowing to
select-insert-update-delete from any table in a mysql database without the
need to create a new query every time. Example:

selectdatas(array('field1','field2','fieldn'),arra y('table1','tablen'),array('left
join,idy','inner join, idx'))

then the function build the query, execute it and then return an object with
the query result.

I must build a database driven site that is 90% of the time spent on
building queries. If I may avoid build them manually it will help me a lot
and let me gain some days of programming.

Bob

May 26 '06 #1
3 2825
Rik
Bob Bedford wrote:
hello

I'm looking for some functions or objects allowing to
select-insert-update-delete from any table in a mysql database
without the
need to create a new query every time. Example:

selectdatas(array('field1','field2','fieldn'),arra y('table1','tablen'),array
('left join,idy','inner join, idx'))

then the function build the query, execute it and then return an
object with
the query result.

I must build a database driven site that is 90% of the time spent on
building queries. If I may avoid build them manually it will help me
a lot
and let me gain some days of programming.


I've tried that in the past. My conclusion was that in most cases, if you
want your queries to be flexible, use joins, where-clauses, etc, you end up
with exactly the same amount or work in writing the queries. Think about it:
if queries could be shorter, the SQL would be shorter. The advantage you do
get is an extra check wether a certain query is allowed to the specific
user, and you can control some of the output.

For a certain project specifically you could always try to figure out which
queries are used several times, and make a template for them. It won't save
you much coding time in my opinion though, but it will make your code more
readable.

Now I have a database object, that:
- takes car of connecting.
- saves querystrings and errors for debugging purposes.
- on selects returns a complete associative array, with (if existing) the
primary key as main key, 0 if no rows match, false if there's an error in
the query.
- on insert returns mysql_insert_id() (or array of insert_id()'s or false on
error.
- on updates and deletes returns the number of rows affected, or false on
error.

One thing that's usefull here that the object calls a certain
database-connection specifically, so it's easier when working with more than
one database in a script.

That's about as much as I can gain from it without inventing my own database
syntax. I'm no genius, and don't think I personally can replace SQL with a
better alternative :-).

Grtz,
--
Rik Wasmus
May 26 '06 #2
Hello Rik, thanks for advice.

In fact SQL is a language that allow to do a lot of different queries. If
the only think it may do is select-insert-update-delete I think it would be
simpler. I'll try or have a look around.

In any case, I've seen on some open-source project that some code to get the
result from a query is one line, as many times the code would be bigger (at
least 2 lines without caring about error handling). I'll start to simplify
this then try to get further.

Thanks again.

Bob

May 26 '06 #3
Rik
Bob Bedford wrote:
Hello Rik, thanks for advice.

In fact SQL is a language that allow to do a lot of different
queries. If the only think it may do is select-insert-update-delete I
think it would be simpler. I'll try or have a look around.

In any case, I've seen on some open-source project that some code to
get the result from a query is one line, as many times the code would
be bigger (at least 2 lines without caring about error handling).
I'll start to simplify this then try to get further.


Yup, terribly simplified for instance:

class database{
var $queries = array();
var $errors = array();
var $connection;
var $print_errors;

function __construct($host, $database,$user='root',$pass=''){
$this->database($host,$database,$user,$pass);
}

function database($host,$database,$user,$pass){
if($connection = @mysql_connect($host,$user,$pass){
$this->connection = $connection;
if($database !=''){
if(!@mysql_select_db($database,$this->connection)){
$this->errors[] = 'Could not select database';
}
}
} else {
$this->errors[] = 'Could not connect to server';
}
}

function print_errors($bool=true){
$this->print_errors = $bool;
}
function log_queries($query, $result='',$error=''){
$log = array('query' => $query, 'result'=>$result,'errors'=$error);
$this->queries = $log;
if($this->print_errors && $error!=''){
print("ERROR:\nquery:$query\nMySQL said:$error");
}
}
function debug(){
print_r(end($this->queries));
}
function debug_all(){
print_r($this->queries);
}
function select_query($query,$index=false){
$result = @mysql_query($query, $this->connection);
if(@mysql_error()){
$this->log_queries($query,'',mysql_error());
return false;
}
if(@mysql_num_rows($result) > 0){
$return_array = array();
while($row = @mysql_fetch_assoc($result)){
if($index){
$return_array[$row[$index]] = $row;
} else {
$return_array[] = $row;
}
}
$this->log_queries($query,$return_array,'');
return $return_array;
} else {
$this->log_queries($query,'','');
return 0;
}
}
}

Now you you can use:

/* initialize object */
$db = new database('host','database','user','pass');

/* option debugging: print all errors, usefull in building fase
this can be turned of by giving false as argument */
$db->print_errors();

/* run query */
$result = $db->select_query('SELECT list, of, fields FROM table',
'optional_field_as_array_key');

/* optional debugging per query */
if($result===false) $db->debug();

/* working with the code */
if(is_array($result)){
//do stuff with it
}

I have never used it but maybe mysqli is something for you:
http://www.php.net/manual/en/ref.mysqli.php

Grtz,
--
Rik Wasmus
May 27 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Alan | last post by:
I have many OR in the if statement, any method to simplify? if (val == 5 | val == 9 | val == 34 | val == 111 | val == 131 | .......) // .... thank you
29
by: Flzw | last post by:
Alright, here is a simple function I coded, won't be hard understanding what it does, and YES, I know, you will probably tell me it's awful code, I would like to know how to write it better, maybe...
3
by: rong.guo | last post by:
Hello group! I am having a problem with simplying my query... I would like to get customers' balance info based on how many months since they opened their accounts. The tricky part here is...
8
by: ben | last post by:
i have a bit of code, that works absolutely fine as is, but seems over complicated/long winded. is there anyway to shorten/simplify it? the code is below. description of it: it's like strcpy in...
6
by: jsceballos | last post by:
Hello. I'm writing a proxy class, i.e: a class whose methods mostly delegate their functionality to other class object. Most of the methods (which are quite a lot) defined in the class would end...
6
by: Patrick | last post by:
Hi All, Kind of new to this. What I have below works, but I am wondering if there is a way to make it more efficient/simpler instead of having to write an if, tr, td, blah for each datatype. How...
0
by: steve728 | last post by:
My current employer has MANY graphically created NESTED JOIN queries. They drive me crazy with their complexities and normally have duplications. Is there a a reference some place on-line or a...
3
by: tshad | last post by:
I have dataGrid that I am filling from a List Collection and need to sort it by the various columns. So I need to be able to sort the Collection and found that you have to set up your own...
1
AmLegacy
by: AmLegacy | last post by:
I'm having a hard time figuring out how to simplify the fractions. Can anyone look at this code and see if you can see something I don't. //This is the fraction adding function void add_fractions...
5
by: Jon Skeet [C# MVP] | last post by:
On Sep 9, 9:41 am, raylopez99 <raylope...@yahoo.comwrote: It's tricky in .NET for two reasons: 1) LINQ doesn't have any concept of "remove" 2) List<T>.RemoveAll doesn't pass in the index...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.