![]() Bases of job with MySQL in PHP In the text translation of the official documentation made by the All-Russia club of Web designers is used. All job with databases is reduced to connection to the server, a choice of a database, a parcel{sending} of search, processing of search and disconnect from bases. So, we shall consider all this put on items{points}. For presentation of the text we shall imagine that by the local machine MySQL server is established. Port for job with it standard. For connection we shall use a login name "root" and the password "no_one". Connection Connection to a database is made with the help of function « mysql_connect () ». To her it is passed three parameters: a name of the server (or a name servera:port for connection), a login name and the password. In case the second call « mysql_connect will be made () » with the same arguments, any new communication{connection} will not be established - instead of it, the identifier of communication{connection} of already open communication{connection} (i.e. job will proceed with the same database) will be returned. If you work only with one database the identifier of connection to create it is not necessary. As soon as performance of the script will end, communication{connection} with the server will be closed, if it has not been obviously closed by earlier call « mysql_close () ». Example: $connect = mysql_connect (' localhost ',' root ',' no_one '); In this case a variable $connect javjaetsja the identifier of connection. If job is conducted only with one base that a code written without the identifier: mysql_connect (' localhost ',' root ',' no_one '); Choice of a database "mysql_select_db" - chooses database MySQL. It means, that on the server can there is not one database, and a little. This command we shall choose what it is necessary (on which at us there are rights). Parameter of this function is the name of base. The base chosen thus becomes active and contacts the certain identifier. If the identifier of communication{connection} is not determined, last connection to base is used. Example: mysql_select_bd (' test ', $connect); - where test this name of a database, and $connect is the identifier of connection. If job is conducted only with one base that a code written without the identifier: mysql_select_bd (' test '); We send search mysql_query () sends search in a database, by present{true} time active on the server which is connected to the certain identifier of communication{connection}. If the identifier is not specified, last open communication{connection} is used. Parameter of this function is the line with sql-search. Example: $tmp=mysql_query (« slect * from table », $connect); - this komnda will give out all contents of the table table from an active database on which specifies the identifier $connect. If job is conducted only with one base that a code written without the identifier: $tmp=mysql_query (« slect * from table »); Processing of search For transaction processing there are some functions. The choice of this or that method of transaction processing depends on style of programming, a task in view. It is necessary to take into account also, that different variants on miscellaneous "load" the server (the some people strongly some people not so). We shall disassemble a little from them. mysql_fetch_object - returns object php as result of processing. This method is good for those who has got used to objective programming Example: while ($result = mysql_fetch_object ($tmp)) echo ($result-> name); mysql_fetch_array - chooses result as an associative file. This method is good for newbies (though how to say, it whom as more is pleasant). Closing of connection Closing of connection is made to means of function mysql_close (). Its{her} parameter is the identifier of connection with a database. If this parameter is not set, last called connection is closed. | |