Monday, December 15, 2008

Php and MYSQL Connection

This problem is about making a connection of php with MySQL, then creating a database and table in it. Then we will add some fields to he table and display the result in html table on browser.

Write the code of php in notepad for making a connection to MySQL. Here is the code of making a connection to MySQL, creating a database and table.

Here connection is being made to the MySQL with the function mysql_connect('servername','username','paswword').This MySQL connect function is put into con variable. In php '$' is used for variables. If else statement is used, if connection is established then Connection established successfully is printed with echo statement otherwise a MySQL error is displayed.

Then a database is created with the function mysql_query("CREATE database name, connection variable).Here connection variable is 'con'.Here also if else statement is used, if database is created then Database created statement is echoed otherwise a error is echoed.

Then 3rd step is to create a table in the database. A table structure is made in the database  naming ‘Persons’ with the statement ‘CREATE TABLE Persons’ and is put into a variable naming ‘sql’. This table has three fields that are: Firstname, Lastname and Age. Names are assigned to ‘varchar’ type and age to ‘int’ type. In the same manner if it creates table then echo statement is used to print Table created, otherwise an error is displayed.

At last connection is closed, after making connection, database and table respectively.



This is the output of the code when executed by giving address in the browser, http://localhost/create.php.

"Connection established, database and table created”.




Next an html form is created which will take firstname, lastname & age. This will call ‘insert.php’ file to submit the data in database with the method 'post'.

Three textboxes are made naming FirstName, LastName & Age. And a Submit button is made. Then all tags are closed. This when executed will display this output.

This is the html form, having three textboxes and a button which will submit the data in database and in other window, fields are inserted and submit is clicked.











    



This html file will call ' insert.php'. In first Martin Pfurner with age 40 is added and in second window Kabir Nanda with age 19 is added.  
       




                                                                         
   

Here in this php code, first connection is made.

Then a particular database is selected, where these entered fields will be inserted using sql statement.

 In values in the sql statement, post method is used for the three fields.After record is added, this gets echoed. And connection gets closed.

 The output of this code is displayed in browser window below:

  

This is the insert.php output and adds the record to the database and displays the statement:

 '1 record added'. 

Now to retrieve these submitted fields, php code is written, which will display the html table having these two fields.
                                                                         
                                                                                 


Here first connection is made, and then a database is selected. A query is written which selects all rows of the table using statement:

mysql_query ("SELECT * FROM Persons");

 Using html, a table is created. A row is created, with three fields in it using 'tr' and 'th' tags of html.

This will fetch the array and will put them in the row, under different headings. These will be echoed.

And table tag is closed. Then MySQL connection is closed. This code when executed, will display the name and age in html table. Its output is:





This way an html table is displayed in the browser by first making a Php and MySQL connection, then creating a database and last table in which fields are entered.






No comments:

Post a Comment