Working with databases The first thing you need to create dynamic pages - a database that will store all the information. Would you like to be able to view the contents of database tables, do the sample, add or update data? All this can be done withyu built in PHP functions for working with databases.
To get started, we must be installed base of PostgreSQL or MySQL. I recommend the latter, as it runs under Unix and Windows, more stable, fast and oshibkoustoychiva. Once again, I will formulate our task: to master the accessiontion to the server, database creation and execution of SQL queries and work with errors. Without knowledge of these actions, we are unlikely to be able to easily run from the database.
So, proceed. Incidentally, hereinafter referred to as I will use MySQL in mind the reasons given above (although with PostgreSQL all the actions are similar, it is necessary only to change the prefix for mysql_ pg_).
Create a database of telephone numbers of staff of your company for our tests:
shell> mysql-u root mysql> create database basa; Query OK, 1 row affected (0.00 sec) mysql> use basa; Database changed mysql> CREATE TABLE phone ( -> Id int (10) NOT NULL auto_increment, -> Name varchar (32) NOT NULL, -> Phone varchar (32) NOT NULL, -> PRIMARY KEY (id) ->); Query OK, 0 rows affected (0.00 sec) mysql> exit Bye
Or use a file from the archive (at the end of article) basa.sql, place it in the MySQL directory and make sure:
shell> mysql-u root <basa.sql I now write a script view.php, which will display data from the database.
<? / * Replace the following variables to their * / $ host = "localhost "; / / MySQL server $ user_db = "root";/ / MySQL user $ pass_db = ""; / / MySQL password $ dbase = "basa";f8000 "> / / MySQL database $ dtable = "phone"; / / Tablein database
/ * Connect to database server * / mysql_connect ($ host, $ user_db t>, $ pass_db); / * Select the database * / mysql_select_db ($ dbase < font color = "# 007700">); / * Creating a SQL query * / $ sql = "SELECT id, name, phone FROM $ dtable" olor = "# 007700">; / * Execution of SQL query * / $ result = mysql_query ($ sqlolor = "# 007700">); / * Check the number of returned rows in a result * / $ rows = mysql_num_rows ( > $ Result); / * If there is no result or a result of less than 1, then to warn us about it or put the result * / 00 "> if ((! $ Rows) | | ($ rows <1)) (echo" Results & nbsp; NO! ";) else ( echo '<table width="309" border="1" cellspacing="1" cellpadding="0"> <tr> <td width="30"> ID </ td> <td width = "166"> Name </ td> <td width="98"> Phone </ td> </ tr> '; while (list ($ id, $ name, $ phone) = mysql_fetch_row ($ result)) ( echo "<tr> <td width = "30"> $ id </ td> <td width=" 166 "> $ name </ td> <td width=" 98 "> $ phone </ td> </ tr> n "; ) ) print "</ table> n"; >
With the launch ofe of the script we will get a page with the text "no results !!!", as in the database these same data are not available. What should I do? Go to the next section and learn how to add data from a form!
Sending data from forms into the database Create onepage entitled forma.html with HTML form and script add.php, which will process the data from this form.
forma.html
<html> <head> <title> Adding data to databaseData </ title> </ head> <body bgcolor="#FFFFFF"> <form name="test" action="add.php" method="post"> <table cellspacing = "2" cellpadding = "2" border = & quot; 0 "> <tr> <td> Name </ td> <td> <input type="text" name="name"> </ td> </ tr> <tr> <td> Phone </ td> <td> <input type = "text" name = "phone & quot;> </ td> </ tr> </ table> <input type="submit" name="submit" value="Dobavit"> </ form> </ body> </ html>
add.php
<? / * Replace the following variables to their * / $ host = "localhost" ont>; / / MySQL server $ user_db = "root";; / / MySQL user $ pass_db = "";; / / MySQL password $ dbase = "basa"; / / MySQL database $ dtable = "phone"; / / Table in database data
/ * Connect to database server * / mysql_connect ($ host, $ user_db 07700 ">, $ pass_db); / * Select the database * / mysql_select_db ($ dbase 00 ">);
$ name = $ _POST [ "name"]; $ phone= "# 007700"> = $ _POST [ "phone"];
/ / Main action script / / Create SQL query $ Sql = "INSERT INTO $ dtable (name, phone) VALUES ( '$ name', '$ phone')";
/ * ExecutionSQL query * / $ result = mysql_query ($ sql); / / Verify andspolneniya operation if (! $ result) ( echo "<H2> Error! </ H2> n"; echo mysql_errno (). ":". mysql_error (). "<P>"; ) Else ( print '<META HTTP-EQUIV="Refresh" CONTENT="2; URL=forma.html">'; print "Record <b> $ name - $ phone </ b> withozdana! "; / / echo phpinfo (); ) >
As you can see, the data from the form (field name and phone) to understand how PHP variables. And ety variables very easily can be added to the database. Now when you browse pages view.phtml you will see that the hand made.
These can handle the script, and as part of URL, if you replace the $ _POST to $ _GET in the body of the script. For example: http://localhost/add.php?name=SomeName & phone = 268-3962 add to the database name SomeName with telephone 268-3962. Incidentally, this is not the phone number of my girls - you can not call!
Dare! And in the next article (soon!), We will continue our "showdown" with PHP. |