YOMEDIA
ADSENSE
Professional PHP Programming phần 4
57
lượt xem 6
download
lượt xem 6
download
Download
Vui lòng tải xuống để xem tài liệu đầy đủ
Trong kịch bản này, chúng tôi có thể sẽ phải tạo ra một userid, ngẫu nhiên, tạm thời và có thể sẽ sử dụng TableDrop () sau khi trật tự được tạo ra để giải phóng tài nguyên hệ thống. Nó là vô cùng hữu ích để sử dụng các yêu cầu () chức năng để kết hợp các mô tả lớp học của bạn và các tờ khai
AMBIENT/
Chủ đề:
Bình luận(0) Đăng nhập để gửi bình luận!
Nội dung Text: Professional PHP Programming phần 4
- Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com PHP Support for Database Connectivity P HP supports API’s for accessing large numbers of databases like Oracle, Sybase, PostgreSQL, MySQL etc. The PHP programs, to access the data from the database on the fly, can use these API's. Open Database Connectivity (ODBC) is a standard Application Programming Interface (API) for accessing a database that has PHP support. This can be used for writing generic database applications. By generic I mean that the same application code will work for all the Databases supporting the ODBC standard. There will be a performance overhead with ODBC, if the database doesn’t support ODBC natively, and moreover ODBC being a generic standard supports only generic features. If you want to use some specific feature of a database, then one should use the language API of that database. In this section we will cover PHP API’s for accessing MySQL databases. You can look at the PHP documentation for APIs to access other databases. Lets briefly cover the features of MySQL before we look into the PHP API’s. MySQL Database M ySQL is a small, compact, easy to use database server, ideal for small and medium sized applications. It is a client/ server implementation that consists of a server daemon m ysqld a nd many different client programs. It is available on a variety of UNIX platforms, Windows NT and Windows 95/98. On UNIX platforms it uses threading, which makes it a high performance and highly scalable database server. T he main features of a MySQL database server are described below. Standards Supported M ySQL supports entry-level ANSI SQL92 and ODBC level 0-2 SQL standard. Language Support T he database server m ysqld c an issue error messages in Czech, Dutch, English, Estonian, French, German, Hungarian, Italian, Norwegian Nynorsk, Polish, Portuguese, Spanish and Swedish. MySQL by default uses the ISO-8859-1 (Latin1) character set for data and sorting. The character set used for data and sorting can be changed while compiling the sources . Programming Language API’s for Clients to Access the Database M ySQL database applications can be written in a set of languages like C, Perl, PHP etc. Large Tables M ySQL stores each table in the database as a separate file in the database directory. The maximum size of a table can be between a minimum of 4GB and the Operating System limit on the maximum file size. Speed, Robustness and Ease of Use M ySQL is about three to four times faster than many other commercial databases. MySQL is also very easy to manage. You do not need a trained Database Administrator for administering a MySQL Installation. TEAM FLY PRESENTS
- Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Cost Advantage M ySQL is an open source relational database. It is distributed free of cost for UNIX and OS/2 platforms and for Microsoft platforms you need to get a license after a trial period of 30 days. So with MySQL you get a cost advantage over other commercial relational databases. Database Features not present in MySQL T hough MySQL is a comprehensive database system, you should be aware of its limitations, which are detailed below. Most of the web based database applications can be written without using these features. But if your application needs these features to be present in the back end database, then you should consider using other commercial databases like SQL Server, Oracle etc., which support these features. Sub-selects S ub-selects are not supported in MySQL. For Example, the following statement returns data about employees whose salaries exceed their department average: SELECT deptno, ename, sal FROM emp x WHERE sal > (SELECT AVG(sal) FROM emp WHERE x.deptno = deptno) ORDER BY deptno; M ost of the SQL statements which use sub selects can be rewritten as SQL statements without sub select. Complex SQL statements using sub selects, which can’t be rewritten to SQL statements without these sub selects should (create and) store the value of the sub query in a temporary table, and access the temporary table in the main query. Transactions A T ransaction is a logical unit of work that comprises one or more SQL statements executed by a single user. A transaction ends when it is explicitly committed or rolled back by that user. F or example in a Banking Application, when a bank customer transfers money from a savings account to a checking account, the transaction might consist of three separate operations: decrease the savings account, increase the checking account, and record the transaction in the transaction journal. When something prevents one of the statements in the transaction from executing (such as a hardware failure), the other statements of the transaction must be undone. Committing a transaction makes permanent the changes resulting from all SQL statements in the transaction. Rolling back a transaction retracts any of the changes resulting from the SQL statements in the transaction. After a transaction is rolled back, the affected data is left unchanged as if the SQL statements in the transaction were never executed. Transactions are currently not supported in MySQL. MySQL supports L OCK_TABLES a nd TEAM FLY PRESENTS
- Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com U NLOCK _ TABLES c ommands to lock tables, which can be used by the thread to prevent interference by other threads for concurrency issues. MySQL does not support Row level locking of tables. L OCK_TABLES c an lock multiple tables with the specified access i.e. Read/ write. Locks on a table get released when the thread holding the lock executes U NLOCK_TABLE c ommand or when the thread holding the lock dies. Stored Procedures and Triggers A s tored procedure is a set of SQL commands that are compiled and are stored in the server. Clients now can refer to the stored procedure, instead of reissuing the entire SQL commands. You get performance benefit by using stored procedures, because the SQL statements are already parsed and compiled in the server, and less data needs to be sent to the server from the client. A trigger is a stored procedure that is invoked when a particular event occurs. For example, a trigger can be set on a stock price table, the trigger gets fired after any U PDATE o peration is done on that table. The trigger can be set to send e-mails to interested people (taken from another table) if the stock prices of any of the updated rows changes by 20%. H owever, MySQL does not have support for stored procedures and triggers. Foreign Keys D ifferent tables in a relational database can be related by common columns, and the rules that govern the relationship of the columns must be maintained. Referential integrity rules guarantee that these relationships are preserved. The column included in the definition of the referential integrity constraint that reference to a primary key of another table is called a foreign key. For example, a D ept c olumn of the E mployees t able is a foreign key that refers to the D ept c olumn of the D epartments T able. Any insert done on the E mployee T able with the wrong D ept c olumn value (i.e. Department does not exist) will fail. It means that there will be no employee entry in the E mployee T able, which will have a department that does not exist. M ySQL does not support foreign keys. However, the foreign key syntax in MySQL does exist, but only for compatibility with other database vendors, and it does not do anything. Views A v iew is a tailored presentation of the data contained in one or more table (or other views). A view takes the output of a query and treats it as a table; therefore, a view can be thought of as a "stored query" or a "virtual table". No storage is allocated to View. For example, in employee table, you want all the users (who are not managers) to see only the name, and employee-id fields of the Table. You can create a view on the table with the following SQL statement: C reate View Employee_View as SELECT name, employee-id FROM E mployee A ll the users (non-managers) can be given SELECT privilege on the Employee_View. Now they will only be able to access name, and employee-id fields of the Employee table. M ySQL d oes not support views. TEAM FLY PRESENTS
- Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com MySQL API Support in PHP mysql_connect C reates a connection to a MySQL Server. int mysql_connect(string [hostname [:port] [:/path_to_socket]], string [username], string [password]); T he arguments for this function are given in the table below, and all of these are optional: P arameter Description Default T he name of the host running the h ostname " localhost" database server. There is no need to specify this if the database and the web server are running on the same machine. T he port that the database server is : port " :3306" using for listening to requests. Only needed if your setup uses a port different than the default for MySQL. T he Unix socket that the server is using : /path_to_sock " :/tmp/mysql.soc et k" for listening to requests. T he name of the user allowed to connect The user that owns the u sername to the database server. web server process T he password for the user; if missing it p assword is assumed empty. T he first argument specifies the hostname (optionally port, else default port is assumed) or the UNIX domain socket, on which the MySQL server is listening for the client requests. If the PHP program and the database server are running on the same machine, then they can communicate using the UNIX domain socket. Note that all the SQL (and other) commands sent to the MySQL server using this connection will be executed with the privileges of the username. The function returns a link identifier (a positive number which references the connection) on success, or f alse o n error. This link identifier will be used in all the function calls, which send requests to the MySQL server. If another m ysql_connect c all is made with the same arguments, a new connection will not be created to the server. The link identifier of the connection already open will be returned. The connection (between the PHP client program and the MySQL Server) will be closed when a m ysql_close c all is made, or when the PHP script exits. TEAM FLY PRESENTS
- Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com mysql_pconnect C reates a persistent connection to a MySQL Server. int mysql_pconnect(string [hostname [:port] [:/path_to_socket]], string [username], string [password]); T he function arguments and the return value are same as those for m ysql_connect . The difference between m ysql_pconnect a nd m ysql_connect i s that the connection created with m ysql_pconnect i s not closed when the PHP program exits or a m ysql_close c all is made. The PHP interpreter maintains the connection with the MySQL server. When a m ysql_pconnect c all is made, the PHP interpreter first finds out if there is an existing open connection with the same function arguments. If it finds one then the link identifier of the existing connection is returned, instead of creating a new connection. The m ysql_pconnect f unction should be used in PHP applications where, over a short period of time, a large number of connections will be made to the MySQL server using the same username and password. m ysql_pconnect s aves the overhead of creating and closing a connection. Note that m ysql_pconnect w ill work only if PHP is configured as a module in the web server. mysql_close T his will end the connection with the MySQL server and is optional. int mysql_close(int [link_identifier]); P arameter Description Default T he reference for the connection to be The link identifier for l ink_identifie r closed. the last connection opened. T he m ysql_close f unction returns t rue o n success, or f alse o n error. Note that m ysql_close w ill not close persistent links generated using m ysql_pconnect . mysql_create_db C reates a new database on the MySQL server. int mysql_create_db(string name, int [link_identifier]); P arameter Description Default T he name of the database to be created. n ame T he reference of the connection, on The link identifier for l ink_identifie r which the request will be sent to the the last connection Database Server. opened. TEAM FLY PRESENTS
- Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com T he n ame p arameter is required, though the l ink_identifier i s optional. The m ysql_create_db() f unction returns t rue o n success, or f alse o n failure. Alternatively m ysql_query c an be used to send the C reate Database S QL command to the MySQL server to create a new database. mysql_drop_db D rops (removes) a MySQL database. int mysql_drop_db(string database_name, int [link_identifier]); P arameter Description Default T he name of the database to be deleted n ame T he reference of the connection, on The link identifier for l ink_identifie r which the request will be sent to the the last connection Database Server. opened. T he n ame p arameter is required, though the l ink_identifier i s optional. The function returns true on success, or false on failure. Alternatively m ysql_query c an be used to send the D rop Database S QL command to the MySQL server to delete a database. mysql_select_db S elects a database as the active database. int mysql_select_db(string database_name, int [link_identifier]); P arameter Description Default T he name of the database which is to d atabase_name become the active database T he reference of the connection, on The link identifier for l ink_identifie r which the request will be sent to the the last connection Database Server. opened. If no connection is open, then the function tries to open a new connection using m ysql_connect w ith default parameters. T he database_ name p arameter is required, though the l ink_identifier i s optional. The function returns t rue o n success, or f alse o n error. TEAM FLY PRESENTS
- Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com A ll the SQL statements passed to the MySQL server will be made on the active database. mysql_query S ends the SQL statement to the MySQL server for execution. int mysql_query(string query, int [link_identifier]); P arameter Description Default T he SQL command to be sent to the q uery MySQL server T he reference of the connection, on The link identifier for l ink_identifie r which the SQL command will be sent to the last connection the Database Server. opened. If no connection is open, then the function tries to open a new connection using m ysql_connect w ith default parameters. T he q uery p arameter is required, though the l ink_identifier i s optional. The function returns a result identifier (positive integer) on success, or f alse o n error. The result identifier contains the result of the execution of the SQL statement on the MySQL server. In the case of Data Definition Language (DDL) SQL statements ( CREATE , A LTER , D ROP ), the result identifier will indicate success or failure. In the case of Data Manipulation Language (DML) SQL statements D ELETE , I NSERT , U PDATE ), the result identifier can be used to find out the number of affected rows by using m ysql_affected_rows() c all, with the result identifier as an argument. With the DML statement S ELECT , the result identifier will be an integer that corresponds to a pointer to the result set. This can be used to find the result of the S ELECT s tatement with a m ysql_result() c all with the result identifier as an argument. For example, the following code creates a table named a ddressbook , which contains the addresses of people. Here the return value of m ysql_query f unction is used to find whether the SQL command execution succeeded or failed. Creating Table
- Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com $hostName = "www.harawat.com"; $databaseName = "php"; $tableName = "addressbook"; S QL statement for creating a table $stmt = "CREATE TABLE %s(NAME CHAR(255), EMAIL CHAR(255), CITY CHAR(255), DESCRIPTION CHAR(255), TELEPHONE CHAR(255), ROWID INT PRIMARY KEY AUTO_INCREMENT)"; F unction to print error messages. function printError($errorMesg) { printf(" %s \n", $errorMesg); } O pen a connection with the database server // Connect to the Database if (!($link=mysql_connect($hostName, $userName, $password))) { printError(sprintf("error connecting to host %s, by user %s", $hostName, $userName)); exit(); } C reate the database $ databaseName . // Create the $databaseName database if (!mysql_create_db($databaseName, $link)) { printError(sprintf("Error in creating %s database", $databaseName)); printError(sprintf("error:%d %s", mysql_errno($link), mysql_error($link))); exit(); } printf(" Created Database %s \n", $databaseName); M ake the created database $ databaseName a s active database. // Make $databaseName the active database if (!mysql_select_db($databaseName, $link)) { printError(sprintf("Error in selecting %s database", $databaseName)); printError(sprintf("error:%d %s", mysql_errno($link), mysql_error($link))); exit(); } C reate the table address book. TEAM FLY PRESENTS
- Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com // Create the table AddressBook if (!mysql_query(sprintf($stmt,$tableName), $link)) { printError(sprintf("Error in executing %s stmt", $stmt)); printError(sprintf("error:%d %s", mysql_errno($link), mysql_error($link))); exit(); } printf(" Created Table %s.%s \n", $databaseName, $tableName); ?> mysql_db_query S ends the SQL statement to the MySQL server, along with the name of the active database. It is similar to m ysql_query . int mysql_db_query(string database, string query, int [link_identifier]); P arameter Description Default T he name of the active database d atabase T he SQL command to be sent to the q uery MySQL server T he reference of the connection, on The link identifier for l ink_identifie r which the request will be sent to the the last connection Database Server. opened. If no connection is open, then the function tries to open a new connection using m ysql_connect w ith default parameters. T he d atabase a nd q uery p arameters are required, though the l ink_identifier i s optional. The return values are same as in the case of m ysql_db_query . For example, to S ELECT a ll rows from the e mployee t able in d atabase1 : $stmt = "SELECT * from employee"; $result = mysql_db_query("database1", $stmt, $linkId); A lternatively mysql_query can also be used by modifying the SQL statement $stmt = "SELECT * from database1.employee"; $result = mysql_query($stmt, $linkId); TEAM FLY PRESENTS
- Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com mysql_list_dbs L ists databases available on the MySQL server. int mysql_list_dbs(int [link_identifier]); P arameter Description Default T he reference for the connection on The link identifier for l ink_identifie r which the request will be sent to the the last connection Database Server. opened. If no connection is open, then the function tries to open a new connection using m ysql_connect w ith default parameters. T he l ink_identifier i s optional. The function returns a result identifier on success, else f alse i s returned on error. The m ysql_tablename() f unction should be used to traverse the result identifier to get the list of databases. mysql_list_tables L ists all the tables in a MySQL database. int mysql_list_tables(string database, int [link_identifier]); P arameter Description Default N ame of the Database, whose list of d atabase tables will be returned. T he reference for the connection on The link identifier for l ink_identifie r which the request will be sent to the the last connection Database Server. opened. If no connection is open, then the function tries to open a new connection using m ysql_connect w ith default parameters. T he d atabase p arameter is required, though the l ink_identifier i s optional. The function returns a result identifier on success, else f alse i s returned on error. The m ysql_tablename() f unction should be used to traverse the result identifier to get the list of databases. mysql_num_rows R eturns the number of rows in the result identifier (which contains the result of the executed SQL TEAM FLY PRESENTS
- Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com s tatement). int mysql_num_rows(int result_identifier); P arameter Description Default T he result identifier returned by r esult_identifie r m ysql_db_query , m ysql_query , m ysql_list_tables , m ysql_list_dbs T he r esult_identifier p arameter is required, and this function is used when the query performed corresponded to a S ELECT s tatement. mysql_tablename G et the table/database name from the result identifier. string mysql_tablename(int result_identifier, int i); P arameter Description Default T he result identifier returned by r esult_identifie r m ysql_list_tables , m ysql_list_dbs T he index in the i r esult_identifier B oth the r esult_identifier a nd i p arameters are required. The function returns the table/database name at index i i n the result identifier. m ysql_num_rows() c an be used to find the number of table/database names in the result identifier. For example, to get the list of all the databases in the MySQL server: List of Databases
- Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com if (!($link = mysql_connect($hostName, $userName, $password))) { printf(" error in connecting to the host %s \n", $hostName); exit(); } G et the list of Databases in the Server // Get the list of Databases if (!($listOfDbs = mysql_list_dbs($link))) { printf(" error in mysql_list_dbs, error %s \n", mysql_error($link)); exit(); } printf(" Databases on %s \n", $hostName); // Get the list of Databases $noOfDbs = 0; D isplay the list of Databases. while ($noOfDbs < mysql_num_rows($listOfDbs)) { printf(" %s \n", mysql_tablename($listOfDbs, $noOfDbs)); $noOfDbs++; } // Free the result pointer mysql_free_result($listOfDbs); ?> mysql_list_fields R etrieves the information about a table. int mysql_list_fields(string database_name, string table_name, int [link_identifier]); P arameter Description Default T he name of the database to which the d atabase_name table belongs T he name of the table about which to t able_name list retrieve the information T he reference for the connection on The link identifier for l ink_identifie r which the request will be sent to the the last connection Database Server. opened. If no connection is open, then the function tries to open a new TEAM FLY PRESENTS
- Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com c onnection using m ysql_connect w ith default parameters. T he d atabase _ name a nd t able_name p arameters are required, though the l ink_identifier i s optional. The function returns a result identifier on success, or f alse o n error. The result identifier can be used with m ysql_field_flags() , m ysql_field_len() , m ysql_field_name() , m ysql_field_type() c alls to get the information about a table. m ysql_list_fields c all is useful in the program, where you don't know beforehand the columns (or data types) of the table. mysql_num_fields G ets the number of fields in a result set. int mysql_num_fields(int result_identifier); P arameter Description Default T he result identifier returned by r esult_identifie r m ysql_db_query , m ysql_query , m ysql_list_tables , m ysql_list_dbs T he r esult_identifier p arameter is required. The function returns the number of fields in the r esult_identifier . mysql_field_len G ets the length of a field. int mysql_field_len(int result_identifier, int field_offset); P arameter Description Default T he result identifier returned by r esult_identifie r m ysql_db_query , m ysql_query , m ysql_list_tables , m ysql_list_dbs T he index for the field in the result f ield_offset identifier T he r esult_identifier a nd f ield_offset p arameters are required. The function returns the length of the field, at f ield_offset i n the r esult_identifier . TEAM FLY PRESENTS
- Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com mysql_field_name R etrieves the name of a field in the database. string mysql_field_name(int result_identifier, int field_index); P arameter Description Default T he result identifier returned by r esult_identifie r m ysql_db_query , m ysql_query , m ysql_list_tables , m ysql_list_dbs T he index for the field in the result f ield_index identifier T he r esult_identifier a nd f ield_index p arameters are required. The function returns the name of the field at offset f ield_index i n the r esult_identifier . mysql_field_type R eturns the data type of a given field. string mysql_field_type(int result_identifier, int field_index); P arameter Description Default T he result identifier returned by r esult_identifie r m ysql_db_query , m ysql_query , m ysql_list_tables , m ysql_list_dbs T he index for the field in the result f ield_index identifier T he r esult_identifier a nd f ield_index p arameters are required. The function returns the type of the field at offset f ield_index i n the r esult_identifier . mysql_field_flags R etrieves the flags for a given field. string mysql_field_flags(int result_identifier, int field_index); P arameter Description Default T he result identifier returned by r esult_identifie r m ysql_db_query , m ysql_query , m ysql_list_tables , m ysql_list_dbs TEAM FLY PRESENTS
- Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com T he index for the field in the result f ield_index identifier T he r esult_identifier a nd f ield_index p arameters are required. The function returns the flags (such as not null, primary key) associated with the field at offset f ield_index i n the r esult_identifier . mysql_field_table R etrieves the name of the table to which a specific field belongs. string mysql_field_table(int result_identifier, int field_index); P arameter Description Default T he result identifier returned by r esult_identifie r m ysql_db_query , m ysql_query , m ysql_list_tables , m ysql_list_dbs T he index for the field in the result f ield_index identifier T he r esult_identifier a nd f ield_index p arameters are required. The function returns the name of the table, for the field at offset f ield_index i n the r esult_identifier . mysql_affected_rows R etrieves the number of rows affected by a SQL query. int mysql_affected_rows(int [link_identifier] ); P arameter Description Default T he reference for the connection on The link identifier for l ink_identifier which the SQL query was sent to the the last connection Database Server. opened. T he l ink_identifier p arameter is optional. The function returns the number of rows affected, in the previous SQL query. This call should be used to find out the number of rows inserted, updated or deleted by the previous SQL ( INSERT , D ELETE , R EPLACE o r U PDATE ) query sent to the server. If the last query was a D ELETE w ithout a W HERE c lause (thus removing all records from a particular table), the function will return zero. The number of rows returned by the S ELECT S QL query should be found with m ysql_num_rows() f unction rather than with m ysql_affected_rows() . mysql_insert_id T his function, whose parameter is optional, Retrieves the auto-increment id generated by the last TEAM FLY PRESENTS
- Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com e xecuted I NSERT S QL command on a table that contained an A UTO_INCREMENT c olumn. int mysql_insert_id(int [link_identifier]); P arameter Description Default T he reference for the connection on The link identifier for l ink_identifier which the INSERT SQL command was the last connection sent. opened. mysql_fetch_row R etrieves the next row from the result identifier, as an enumerated array. array mysql_fetch_row(int result_identifier); P arameter Description Default T he result identifier returned by r esult_identifie r m ysql_db_query , m ysql_query , m ysql_list_tables , m ysql_list_dbs T he r esult_identifier p arameter is required. The function returns an array (corresponding to the current row ), or f alse i f there are no more rows. m ysql_fetch_row() i nternally increments the internal row pointer field of the r esult_identifier . So each subsequent call of m ysql_fetch_row() w ill return the next row from the result. mysql_data_seek S ets the internal row pointer of the result identifier. int mysql_data_seek(int result_identifier, int row_number); P arameter Description Default T he result identifier returned by r esult_identifie r m ysql_db_query , m ysql_query , m ysql_list_tables , m ysql_list_dbs T he index of the row to which to set the r ow_number pointer. T he r esult_identifier a nd r ow_number p arameters are both required. The function sets the internal row pointer of the r esult_identifier t o r ow_number . The next call to m ysql_fetch_row w ill return r ow_number r ow. TEAM FLY PRESENTS
- Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com T he function returns t rue o n success, or f alse o n error. mysql_fetch_field R etrieves the column information from the result set. object mysql_fetch_field(int result_identifier, int [field_offset]); P arameter Description Default T he result identifier returned by r esult_identifie r m ysql_db_query , m ysql_query , m ysql_list_tables , m ysql_list_dbs T he index for the field in the result The next field not f ield_offset identifier retrieved with m ysql_fetch_f ield T he r esult_identifier p arameter is required, though f ield_index i s optional. The function returns an object describing the field at offset f ield_offset , in the r esult_identifier . If the optional argument f ield_offset i s not specified, then the next field that was not retrieved with m ysql_fetch_field i s returned. The returned object has the following properties: n ame: column name ❑ t able: name of the table the column belongs to ❑ m ax_length: maximum length of the column ❑ n ot_null: 1, if the column cannot be null ❑ p rimary_key; 1, if the column is a primary key ❑ u nique_key: 1, if the column is a unique key ❑ m ultiple_key: 1, if the column is a non-unique key ❑ n umeric: 1, if the column is numeric ❑ b lob: 1, if the column is a BLOB ❑ t ype: the type of the column ❑ u nsigned: 1, if the column is unsigned ❑ z erofill: 1, if the column is zero-filled ❑ Example SELECT employee.name, department.deptname FROM employee, department WHERE emplyee.deptno = department.deptno TEAM FLY PRESENTS
- Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com I f m ysql_fetch_field() is called on the result identifier containing the result of the above query, then the first call will return the description of e mployee.name f ield, and the second call will return the description of d epartment.deptname f ield. mysql_field_seek T he function sets the f etch_field o ffset of the r esult_identifier t o f ield_offset . int mysql_field_seek(int result_identifier, int field_offset); P arameter Description Default T he result identifier returned by r esult_identifie r m ysql_db_query , m ysql_query , m ysql_list_tables , m ysql_list_dbs T he index for the field in the result f ield_offset identifier T he f ield_offset a nd r esult_identifier p arameters are both required. The next call to m ysql_fetch_field() w ill return the object describing the f ield_offset , field of the result identifier. The function returns t rue o n success, else f alse o n failure. mysql_fetch_object R eturns an object that corresponds to the fetched row from the result identifier. object mysql_fetch_object(int result_identifier, int [result_type]); P arameter Description Default T he result identifier returned by r esult_identifie r m ysql_db_query , m ysql_query , m ysql_list_tables , m ysql_list_dbs A c onstant indicating what type (or types) r esult_type M YSQL_ASSOC of array to return T he r esult_identifier p arameter is required. The optional argument r esult_type c an have the following values (similar to m ysql_fetch_array ): ❑ M YSQL_NUM ❑ M YSQL_ASSOC ❑ M YSQL_BOTH TEAM FLY PRESENTS
- Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com T he function is similar to m ysql_fetch_array , except that an object is returned, instead of an array. Therefore only M YSQL_ASSOC o r M YSQL_BOTH w ill make sense, because numbers cannot be names for object properties. Therefore, if you need to access fields with the same name in different tables, you will need to alias them. If you used the S ELECT q uery: SELECT tab1.id AS id1, tab2.id AS id2 (...) T hen you can access the results by using: $result = mysql_query("SELECT (...)"); $row = mysql_fetch_object($result); A nd then referring to $ row->id1 a nd $ row->id2 w ill return the corresponding result field. Example I n the above example, the columns of the rows are accessed by field names. mysql_fetch_array F etches the row as an associative array. array mysql_fetch_array(int result_identifier, int [result_type]); P arameter Description Default T he result identifier returned by r esult_identifie r m ysql_db_query , m ysql_query , m ysql_list_tables , m ysql_list_dbs A c onstant indicating what type (or types) r esult_type M YSQL_BOTH of array to return T he r esult_identifier p arameter is required. The second optional argument r esult_type c an have the following values TEAM FLY PRESENTS
- Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com M YSQL_NUM : Will cause the returned array to contain numeric indices only (similar to ❑ m ysql_fetch_row() ) M YSQL_ASSOC : Will cause the returned array to contain associative indices only ❑ M YSQL_BOTH : Will cause the returned array to contain both numeric and associative indices ❑ If the second argument r esult_type i s not specified, then M YSQL_BOTH i s assumed as the value for the second argument. Each subsequent call of m ysql_fetch_array() w ill return an array corresponding to the next row, or f alse i f there are no more rows. This is an extended version of m ysql_fetch_row() , which returns only a numerically indexed array. By contrast m ysql_fetch_array() r eturns the results also as an associative array using the field names as keys, this without incurring any performance penalties. The limitation of the associative array is that if there is duplication in the field names, the last one will take precedence and you will need to use the numerical index to extract the other fields with the same name, or use aliasing of the result fields. For example, if the SQL query was: SELECT tab1.id, tab2.id (...) FROM tab1,tab2 (...) A nd you used: $result = mysql_query("SELECT (...)"); $row = mysql_fetch_array($result); T hen, referring to $ row["id"] w ill return the contents of t ab2.id . To access t ab1.id , we can use $ row[0] . Alternatively, if your SQL query was: SELECT tab1.id as id1, tab2.id as id2 (...) T hen you will be able to refer to $ row["id1"] a nd $ row["id2"] t o access the corresponding field. Example TEAM FLY PRESENTS
ADSENSE
CÓ THỂ BẠN MUỐN DOWNLOAD
Thêm tài liệu vào bộ sưu tập có sẵn:
Báo xấu
LAVA
AANETWORK
TRỢ GIÚP
HỖ TRỢ KHÁCH HÀNG
Chịu trách nhiệm nội dung:
Nguyễn Công Hà - Giám đốc Công ty TNHH TÀI LIỆU TRỰC TUYẾN VI NA
LIÊN HỆ
Địa chỉ: P402, 54A Nơ Trang Long, Phường 14, Q.Bình Thạnh, TP.HCM
Hotline: 093 303 0098
Email: support@tailieu.vn