Selecting and Displaying records from database using php and mysql

Selecting and Displaying records from mysql table using php is quite easy.

These are the steps that should be followed.
  1. Include connection file.
  2. Write and execute query.
  3. Apply while loop to get all results

Please see the images in order to completely understand how to select and display the records using php and mysql



Code




mysql_fetch_array() is used to fetch regular array, row by row from the result set and this process goes on until it reaches the last row if this function is inside while loop, if this function is not inside while loop then it fetchs only one row from the result set and focus move to the next code segment of the php program.

In our php program we need to display all the records that match our query so we use mysql_fetch_array() inside the while loop and also display every row one by one using echo statement.

Let's understand php code use for selecting and displaying records from mysql database

require_once('conn.php');
we first include the connection file in our php program. In this connection file we define host which is localhost, user which is by default root, database name and then mysql_connect() function to establish connection with database and then mysql_select_db() function to select the database. To know exactly how to create connection with database using php please read this post.

$select_query="select * from student";
We write a query to select all the records from the student table and then assign this query to a variable $select_query

$result=mysql_query($select_query);

mysql_query($select_query) is used to execute the query, it takes query as a parameter, in the above line the query is executed and result set is store inside the variable $result. and then we apply mysql_fetch_array() to display rows from resultset one by one. mysql_fetch_array() takes result set as a parameter.




Output


Read more...

Insertion of records in database using php and mysql

In this tuorial we will learn how to insert records in mysql database.


To insert records in a database table, required these steps


  1. First of all create the database if you want to use new database.
  2. create tables if u want to use new table in which values will be inserted.
  3. create connection file.
  4. create a php file that contains a form using this form values will be inserted to the database.

Process of Inserting Records in a database table

Values goes to the next page, the page defined in the action attribute, after the form is submitted.

  1. Values are recieved by using $_GET or $_POST
  2. Write SQL Query.
  3. Execute the Query.

Please see the images to clearify the data insertion concept and then check explaination of all this process.

insert-records.php

In this php file we creates a form that contains two fields one is Name and other is FatherName.

To get Name, we have textbox and the name attribute of textbox is name. I think you are confusing???

Ok Listen, the name attribute of the field is the really big boss who stores the value whatever user writes in the textbox, for the Name of user, i place a textbox and give its name attribute a value called name, now this textbox will be accessed in the next page, whenever form is submitted by its name attribute, in this case its name=name.

Same for Father Name, we have a textbox whose name attribute is fname and this fname make it possible to access the value of this textbox in the next page.

action="do-insert.php", this is the page where form elements will be accessed and insertion will placed.

method=post means that form values will be transfered using post method.



do-insert-records.php

In this php file, first of all i include the connection file, then form elements that are coming from previous page are accessed and stored in the variables.

Then i write the sql query and save it in a variable called $queryinsert. and for executing this query, i passed $queryinsert as a parameter and execute the query.

Note:- mysql_query() is used to execute the mysql query.

After executing the insertion query i save the resultset in a variable $resultset and then check whether resultset is true or not, if resultset is true then its mean our query has been successfully executed, else we are going to print the error to find out why query is not executed.



conn.php



Output

Read more...

How to make connection with mysql

Suppose you have mysql and php installed, also you they both are interacting with each other then here is the connection file that u have to made in order to interact with the database of mysql.


$host="localhost"; \\ if the database you want to interact is on your pc.



$user="root"; \\ mysql username, by default is root



$pass="yourpassword"; \\ pass of mysql, if there is no password then $pass="";



$db_name="yourdatabase";



mysql_connect("$host","$user","$pass") or die("cannot connect to the database server");



mysql_select_db("$db_name") or die("cannot select the database");



save this in any file, such as conn.php and include in every file over which u want database interaction.



Read more...

File handling in php

php gives u facility to read, write, open, close and perform various operation over files.

The function use to open the files in php is fopen().

It contains two parameters. The first determines the name of the file with extension and second parameter determines the mode in which the file should open. Like read, write etc.

Example:- Opening of file using fopen()

$file_open=fopen("myfile.txt","r");

The above code is responsible for opening the text file in readonly mode, means u cannot perform any write operation on this file.

Different modes for opening the file are

  1. r+ (Read/Write. Start Reading Writing from the beginning of the file)
  2. w (Write. Open the file and write over file, if file doesn't exist then also creates the file)
  3. w+ (Read/Write.Open the file and write over file, if file doesn't exist then also creates the file)
  4. a (Append. Open the file and write at the end of the file, if file doesn't exist then also creates the file)
  5. a+ (Append/Read. Preserves the contents of the file by writing at the end of the opened file)
  6. x (Write. Creates the new file. Send error as well as return false if the file has been already created).
  7. x+ (Read/Write. Creates the new file. Send error as well as return false if the file has been already created)

If fopen() function unable to open the file it returns false.

Displaying message if file is not opening

$file_open=fopen("myfile.txt","r") or exit("unable to open the file");

Checking End-Of-File (EOF)

Sometime u doesn't know how many lines the file contains that u want to read, for this you use feof() that checks if the End-Of-File has been reached or not. It is better approach to use this during looping process.

if(feof($file_open))

{

echo "End of file has been reached";

}

Closing of file

$file_open=fopen("myfile.txt","r") or exit("unable to open the file");

fclose($file_open);

Reading data line by line from file

The fgets() function is used to read a single line from opened file.

Example:-

$file_open=fopen("myfile.txt","r") or exit("unable to open the file");

while(!feof($file_open))

{

echo fgets($file_open)."\n";

}

fclose($file_open);

Reading a file Character by Character

The fgetc() function is used to read a single line from opened file.

Example:-

$file_open=fopen("myfile.txt","r") or exit("unable to open the file");

while(!feof($file_open))
{
echo fgetc($file_open)."\n";
}
fclose($file_open);



.txt file

.php file


Output

Read more...

Date() handling in php

The php date() function is used to show dates and time.

The date() function takes two parameters, one is required and second one is optional.

Parameters of date()
  1. format. Its Required.Specify the format in which timestamp or date should be display.
  2. timestamp. Its Optional. It specifies the timestamp. Default timestamp is the current date and time.

Timestamp????

A timestamp is the numbers of seconds since jan 1,1970 at 00:00:00 gmt. It is also called Unix Timestamp.

How to format date using date()

The first parameter in the date() tells the format of the date to be displayed. It use different letters to represent date and time formats. Some are given below:

  1. d- The day of the month (01-31)
  2. m- The current month in the form of number (01-12)
  3. Y- The current year in the form of four digits like 2009

You can characters like "-". "/" between the letters to make dates and time stamp more readable.

Examples:-

echo date("Y-m-d");

echo date("Y/m/d");

Adding a Timestamp

The second parameter that used in the date() represents a timestamp. Its optional, if it is missing then current time will be used.

we normally use mktime() to create timestamp for future dates like tommorow or day after tommorow and so, on.

the mktime() function return the unix timestamp for the date specified.

To see what is date at tomorrow.

$date_tommorow=mktime(0,0,0,date("m"),date("d")+1,date("Y"));

echo "The date for tommorow is".$date_tommorow;

There are lot of functions that deals with dates and time in php, i will tell u about these in my post later.

Read more...