[SOLVED] Generic Mysql Application in PHP
Posted: Wed Aug 01, 2007 3:51 am
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???
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>";
}
?>