Designing a generic mysql query application

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Locked
dream2rule
Forum Contributor
Posts: 109
Joined: Wed Jun 13, 2007 5:07 am

Designing a generic mysql query application

Post by dream2rule »

Hello Friends,

I have been given a task to code for a generic mysql application where in i need to use functions in PHP to insert, update, delete etc (MySQL querying) so that i can use this code as a reusability tool for designing any database in MYSQL.

I just started with it and i got it half way. I create the respective functions and within those i write the associated query to insert, update or delete records. The table name and the field name is being sent to a function via arguments.

I just include the file and call the respective functions where i need to perform the given tasks.

But I am stuck.. as to i am unable to figure out the same when there are multiple field names and multiple data items involved.

Here's my code in which i am using the functions only for a single record. How do i go aout when there are multiple field names and multiple records to be inserted or updated in a database???

Code: Select all

<?php

//function to insert a single value into the database
function insert($table_name,$column_name,$value)
{
	$sql_insert = "INSERT INTO $table_name ($column_name) VALUE ('$value')";
	echo $sql_insert."<br>";
	mysql_query($sql_insert);
}

//function to update a single value into the database
function update($table_name,$column_name_1,$new_value,$column_name_2,$original_value)
{
	$sql_update = "UPDATE $table_name SET $column_name_1='$new_value' WHERE $column_name_2='$original_value'";
	echo $sql_update."<br>";
	mysql_query($sql_update);
}

//function to delete a single value into the database
function delete($table_name,$column_name,$value)
{
	$sql_delete = "DELETE FROM $table_name WHERE $column_name='$value'";
	echo $sql_delete."<br>";
	mysql_query($sql_delete);
}

//function to find the maximum value
function maximum_value($column_name,$table_name)
{
	$sql_max = "SELECT MAX($column_name) FROM $table_name";
	$rs_max = mysql_fetch_row(mysql_query($sql_max));
	echo "Maximum value of $column_name is ".$rs_max[0]."<br>";
}


//function to count the number of rows in a database
function count_rows($table_name)
{
	$sql_count = "SELECT count(*) FROM $table_name";
	$rs_count = mysql_fetch_row(mysql_query($sql_count));
	echo "Total number of rows in $table_name is ".$rs_count[0]."<br>";
}

?>
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

You could use hashes for $column_name, $value parameters and iterate with foreach.
There are 10 types of people in this world, those who understand binary and those who don't
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

You should stop multiposting.
Locked