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.
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.
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.
This is the insert.php output and adds the record to the database and displays the statement:
'1 record added'.
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:
No comments:
Post a Comment