Skip to content

Opensource project ,which aims to provide php code shared among programmers.Anybody can contribute to this project.

Notifications You must be signed in to change notification settings

jinujd/essentials.php

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

essentials.php  is the opensource project aimed at sharing genereal purpose php code.

Manual for current version
------------------------
1)sql_interface
    sql_interface is a class defined in essentials to make MySQL database access more easier.
	First we need to create the object of the class using the following constructor
	$sql = new sql_interface($db_uname,$db_pwd,$db_host);
	 , where  $db_uname the username for MySQL database,$db_pwd is the password for MySQL batabase and $db_host is the hostname.
	 We suggest to include this in a separate file connect.php,so that everytime we just need to include that file for connection.
	 
	 
	 a)Creating a database.
	  To create a new database , we have to use the add_db(<dbName>) function.For ex if let 'myNewDb' be your database name.Then it can be created as
	  $sql->add_db('myNewDb');
	  The database will not be created ,if there exists a database with same name.
	  After creation of the database ,the newly created database is selected implicitely.
	 b)Selecting a different database.
	     To select a different database we use the db(<dbName>) function.Suppose we need to select the database named 'myNewDb1'.It can be done as follows.
      $sql->db('myNewDb1');
	  Returns false if database does not exist
     c)Creating a table.
        A table can be created inside the selected database using the add_table(<tableName>,<details>) function
        For example suppose we need to create a table  named 'myTable' with following columns.
        regNo int, username text ,dob DATE
		Then it can be done as
        $sql->add_table('myTable','regNo int, username text ,dob DATE');
        After the creation of the table,the newly created table will be selected automatically , for further access. 
	    The table will not be created ,if the table already exists		
	d)Creating multiple tables
	     Multiple tables can be created using the add_tables(<tables>) function.
         For example if we need to create the following two tables
		 table1 - regNo int, username text ,dob DATE
		 table2 - regNo int
		 It can be done using the query.
		 $sql->add_tables(array('table1' => 'regNo int, username text ,dob DATE','table2' => 'regNo int' ));
	e)Selecting a different table.
	    A different table can be selected using the function table(<tableName>);
		For ex: to select table named 'myTable1',the code will be
		$sql->table('myTable1');
	f)Adding a record to a table
	   A record can be added to a table using add_record(<values>) function
	   Values is an array storing values for columns.It can be of two types 
			   1) if it is a normal array ,each element are considered as values of the column names in the order they appear in the table
			   2) it can be specified as an associative array as ["<column name1>" => <value1>,"<column name2>" => <value2>,...]
			   
	g)Deleting record(s)
	    Record(s) can be deleted from a table using the drop_records(<condition>).If no condition is given ,all records will be deleted from the table.
         ex:$sql->table('myTable1')->drop_records('age > 30');
              will remove all records from the table 'myTable1' having the value of column named 'age' is >30
			 $sql->table('myTable1')->drop_records(); 
			   will remove all records from the table 
    h)Updating record(s)
        Records can be updated using the update_record(<vales>,<condition>)	.
		$values = [],associative array in the format [<column name>=><value>,<column name1>=><value1>,...].
		If condition is not given,all records will be updated
		ex:$sql->table('myTable1')->update_records(array('age'=>45,'name'=>db_str('unknown')));
		      updates the column age as 45 and name as 'unknown' for every record
			  [Note:Columns with string(VARCHAR,TEXT)/Date data types should be converted to a database string using the function db_str().
			  Otherwise error will occur ]
			$sql->table('myTable1')->update_records(array('age'=>45,'name'=>db_str('unknown')),'age > 40');
		      updates the column age as 45 and name as 'unknown' for every record with value of column 'age' > 40
     i)Selecting(Filtering recors from a table)
	     Records can be selected using the select(<columns>,<condition>) function.
              <columns> is array stroing names of columns to select.If condition is not given, all records in the specified columns will be selected.
			  ex:$sql->table('myTable')->select(array('*'),'WHERE age>10 order by age');
			  selects all columns for records satisfying the condition.
			  $sql->table('myTable')->select(array('name','age'),'WHERE age>10 order by age');
			   selects columns name and age for records satisfying the condition.
	 j)Fetching the selected records
<<<<<<< HEAD
=======
               Selected records can be fetched using the fetch() function.
                  It returns a row from the selected result set and returns it. If there is only one column in a row ,the value 
of the column willl be returned.Other wise an array ,is returned whose ith element is the value of ith column in the result
.After each fetch ,the row pointer will be updated to the next row in the result.If there is no rows remaining,fetch()
will return false.
ex:$sql->table('myTable')->select(array('*'),'WHERE age>10 order by age');
$sql->fetch() 
    OR 
$sql->table('myTable')->select(array('*'),'WHERE age>10 order by age')->fetch()
    will fetch the first row from the selected result

>>>>>>> 19fb9a8df4062376bb4e108eb580590ca58d0a69
		
			
	f)Creating a view
	    A view can be created using create_view(<viewName>,<result>).For example if we need to 
		

About

Opensource project ,which aims to provide php code shared among programmers.Anybody can contribute to this project.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages