Writing functions to insert, update, search, delete from SQL tables in PHP
Beginners who have started learning any programming language, some time find difficulties searching good material to start with. In today's post we will write some basic functions in PHP to insert, update, search and delete from SQL tables. Writing functions are important because once you have function created you don't have to write queries again and again only call the function.
First connect to your server
These are the few functions to get start with PHP as you start learning it deeply you can add functions to your collection and modify them according to your requirement. If you have any question you can ask in the comments below.
First connect to your server
$host = "Your host name"; $user = "User name for host"; $pass = "Password for user"; $database = "Name of database"; $connect= mysqli_connect($host,$user,$pass); mysqli_select_db($database ,$connect);Once database is connected we can write functions.
Function to insert
Basic query to insert SQL table ismysqli_query("INSERT INTO Persons (FirstName, LastName, Age) VALUES ('Glenn', 'Quagmire',33)");We will write function to insert values in SQL table
function InsertInTable($table,&$fields){ $col = "insert into $table (`".implode("` , `",array_keys($fields))."`)"; $val = " values('"; foreach($fields as $key => $value) { $fields[$key] = mysqli_escape_string($value); } $val .= implode("' , '",array_values($fields))."');"; $fields = array(); return; }Now where you want values to insert in table call the function and add values in array like below
$field['FirstName'] = 'Glenn'; $field['LastName'] = 'Quagmire'; $field['Age'] = '33'; InsertInTable("Persons",$field);
Function to Update
Basic query to update SQL table with PHP ismysqli_query("UPDATE Persons SET Age=36 WHERE FirstName='Glenn' AND LastName='Quagmire'");We will create function to update SQL table will be like this
function UpdateTable($table,&$fields,$condition) { $sql = "update $table set "; foreach($fields as $key => $value) { $fields[$key] = " `$key` = '".mysqli_escape_string($value)."' "; } $sql .= implode(" , ",array_values($fields))." where ".$condition.";"; $fields = array(); }Where you want to update values in table call function like this
$field['Age'] = '36'; UpdateTable("Persons",$field," FirstName= 'Glenn'");
Function to Select
Basic query to select record from table is$result = mysqli_query("SELECT * FROM Persons"); while($row = mysqli_fetch_array($result)){ echo $row['FirstName'] . " " . $row['LastName']; echo "Function to select record will be like this
"; }
function SelectTableRecords($query){ $result = mysqli_query ($query); $count = 0; $data = array(); while ( $row = mysqli_fetch_array($result)){ $data[$count] = $row; $count++; } return $data; }To use this function you will write like this
SelectTableRecords("SELECT * FROM Persons"); //for condition put WHERE clause after table name
Function to Delete
Basic query to delete ismysqli_query("DELETE FROM Persons WHERE LastName='Griffin'");Function to delete any record will be like this
function DeleteRecord($query){ $result = mysqli_query ($query) return true; }Use this function like below
DeleteRecord("DELETE FROM Persons WHERE LastName = 'Griffin'");
These are the few functions to get start with PHP as you start learning it deeply you can add functions to your collection and modify them according to your requirement. If you have any question you can ask in the comments below.

Abida
Abida is web developer works with HTML5, Javascript, PHP. She has developed many websites and now shares her knowledge and tips with Knowledge Aspire.
Get latest updates in your inbox
Popular Posts
How Yoga Nurtures Your Programming Skills
Understanding and analyzing basics of SEO
Creating SEO friendly URLs with PHP and SQL
Adobe AIR - Developing cross platform applications
Writing functions to insert, update, search, delete from SQL tables in PHP
Getting started with C#
Creating XML sitemap with PHP and SQL and submitting to search engines
Creating RSS feed in xml format with PHP and SQL
Creating thumbnails dynamically from images with PHP
Backup SQL database with PHP
Tags
CSV
Validation
Android
Java
Nashorn
PHP Mailer
cPanel
Facebook
Marketing
AdSense
Google
Technorati
Feathers
Starling
crop
PDF
Yoga
feedburner
rss feed
meta tags
SQL
backup database
Adobe Air
XML Sitemap
Syntaxhighlighter
Uploadify
Tinymce
screenshot
Text image
webmaster tools
Verifying website
web analysis
SEO
visual studio 2013
IDE