We use base MySQL
mysql - the Control system of Databases (SUBD). The basic difference from all others SUBD it that she is free-of-charge. By virtue of that mysql is free-of-charge, she is supported very much by providers much{many} a hosting.
Here I shall tell as to connect php and mysql i.e. how to obtain the data from a database mysql in php a script.
Well generally it not so is difficult. Everything, that it is necessary to know:
Host - the address of the server of databases mysql
Name of a database
Login name
The password - the password for access to a DB
The some people sql commands
Further I shall assume, that at you the database on khostera server or on a domestic computer is already got{started}
(mysql it is possible to download from a site http://mysql.com <http: // www.internet-technologies.ru/? url=http%3A%2F%2Fmysql.com>).
So, algorithm such:
We establish{install} connection with the server
We choose the necessary database
We interpellate to the server of databases (sql search)
We process result of search if it is necessary
We close connection (we are disconnected from the server of a DB)
In connection with php to work from a DB it is necessary to know some functions (the full list look in php a manual).
mysql_connect ();
mysql_select_db ();
mysql_query ();
mysql_fetch_array ();
mysql_close ();
It is that minimum which will allow you to establish connection with mysql server,
To choose databases and to do{make} searches to the server of databases.
For the information{inquiry}!
All php functions which are intended for job with a database mysql have a prefix mysql_
I shall not consider{examine} syntax of each function. I hope at you is php manual
(it{he} can be downloaded from a site http://php.net <http: // www.internet-technologies.ru/? url=http%3A%2F%2Fphp.net>). We shall consider these functions in business i.e. on an example.
We admit{allow} in our database to be stored{kept} one table with the following structure:
Name of a field type (length) the description
id whole (2) unique number{room}
name a symbolical (100) name
tel symbolical (20) phone
The table has a name: customer, and our database is called database.
We admit{allow}, that your data for connection to mysql server Such:
Host: localhost
Login name: guest
The password: mypassword
For the beginning it is necessary to create our table customer. We shall write a script which will create in a database database
The table customer. We shall name a file as install.php.
File install.php
<? php
// The data for mysql the server
$dbhost = "localhost"; // the Host
$dbuser = "guest"; // the Login name
$dbpassword = "mypassword"; // the Password
$dbname = "database"; // the Name of a database
// We are connected to mysql to the server
$link = mysql_connect ($dbhost, $dbuser, $dbpassword);
// We choose our database
mysql_select_db ($dbname, $link);
// We create the table customer
// I.e. we do{make} sql search
$query = " create table customer (id int (2) primary key
auto_increment, name varchar (100), tel varchar (20)) ";
mysql_query ($query, $link);
// We close connection
mysql_close ($link)
?>
Function mysql_connect () returns the identifier of connection. It is necessary to specify this identifier in all mysql functions. It is possible to lead{carry out} analogy to the index on a file (file pointer) which is used by functions on job with files. In parameters of function we have specified a host, a login name and the password to a database.
On mysql server to be stored{kept} not only your database. The server can serve thousand such databases. Therefore, to get access to the database, it is necessary to choose her . The choice of a database is carried out by function mysql_select_db (). In parameters it is underlined: a name of a required database $dbname and the identifier of connection $link, which we have received with the help of function mysql_connect ().
After we were connected to mysql server and have chosen our database, we do{make} sql search. On language sql you will find the literature on a site mysql.ru. In search we specify, that we want to create the table with a name customer and with structure resulted in the table is higher. The search to mysql server is carried out with the help of function mysql_query (). In parameters the search $query and the identifier of connection $link is underlined.
And, at last, we are disconnected from the server (we close connection) by function mysql_close (). In parameters we specify the identifier of that connection which needs to be closed.
For the information{inquiry}!
Actually the script can establish some connections with mysql server.
All depends on a hosting on which you sit. If all of you have made correctly on mysql server in your database, the table customer will appear.
After we have created the table, her it is necessary to fill i.e. write down in it{her} any given (recordings). We shall write a script which will add recording in our table. We shall name a file of a script as insert.php.
File insert.php
<? php
// The data for mysql the server
$dbhost = "localhost"; // the Host
$dbuser = "guest"; // the Login name
$dbpassword = "mypassword"; // the Password
$dbname = "database"; // the Name of a database
// We are connected to mysql to the server
$link = mysql_connect ($dbhost, $dbuser, $dbpassword);
// We choose our database
mysql_select_db ($dbname, $link);
// We add recording in our table customer
// I.e. we do{make} sql search
$query = " insert into customer values (0, 'Ivanov Ivan Ivanovich',
' (095555-55-55 ') ";
mysql_query ($query, $link);
// We close connection
mysql_close ($link);
?>
Having seen this script you zametete, that he differs from previous only in the line of search $query.
Now when we are able to write down the data in a database, we shall pass to procedure of search of the data from base mysql. For this purpose we shall write a script and we shall name a file select.php.
File select.php
<? php
// The data for mysql the server
$dbhost = "localhost"; // the Host
$dbuser = "guest"; // the Login name
$dbpassword = "mypassword"; // the Password
$dbname = "database"; // the Name of a database
// We are connected to mysql to the server
$link = mysql_connect ($dbhost, $dbuser, $dbpassword);
// We choose our database
mysql_select_db ($dbname, $link);
// We add recording in our table customer
// I.e. we do{make} sql search
$query = " select * from customer ";
// We require
$result = mysql_query ($query, $link);
while ($rows = mysql_fetch_array ($result, mysql_assoc))
{
printf (" id: % d, name: % s, tel: % s ",
rows ['id'], rows ['name'], rows ['tel']);
}
// We close connection
mysql_close ($link);
?>
Well here... All is understandable:):):) the line of search $query has naturally changed,
Which now contains the sql-operator select. As the small code while () {was added....}. I shall say only, that function mysql_fetch_array () processes result of search and returns a file of fields current! Attention! The current line of result.
The constant mysql_assoc specifies that function should return an associative file of fields. After function mysql_fetch_array () will process all the line long result, she will return value false and then cycle while () - to not be executed.
Here in a cycle we use function printf (). She is very similar to the same function in language c. The information{inquiry} on function printf () can see in php a manual.
I hope my clause{article} though has a little helped you to understand communication{connection} php and mysql.

|