MySQL fetch array processes the results of fetching from the database and returns as a result a regular, associative or both arrays at once. In fact, MySQL fetch is a translation of information received upon request to the database into a form convenient for processing.
On simple queries, the syntax of one sampling line is turned into an array; on stream queries, this is a tool for sequential processing of the information stream.
Syntax and Application Example
The input to the PHP MySQL fetch array receives the query result and the format of the desired result can be specified:
- MYSQL_ASSOC;
- MYSQL_NUM;
- MYSQL_BOTH.
In the first case, an associative array will be formed, in the second case, an array with numerical indices, and in the latter case, an array with indices of both types will be available.
As a result of the execution of $ aFetch = mysqli_fetch_array ($ cResult), an array of $ aFetch will be created .
In this example, MySQL fetch array processes the raw data (1). There are only four entries in the table for each of the various fetch options. The result of processing the selection (2) is stitched into data rows (3): $ cFetch, $ cFetch1, $ cFetch2, $ cFetch3.
The results show that without specifying the constants MYSQL_ASSOC, MYSQL_NUM and MYSQL_BOTH, the same result is obtained as in the case of MYSQL_BOTH. These constants are deprecated and their use is not recommended.
Using MySQL fetch array
Using query results in array format is very convenient in PHP programs. The MySQL database access functionality provides safe and reliable access methods.
MySQL fetch array generates a warning level error only in a situation where $ cResult is absent or invalid, and therefore, the connection to the database did not take place. The name $ cResult can be anything, here it is used in the context of the example. In request (2):
- $ cResult = mysqli_query ($ this-> iConnectId, $ cQuery);
the variable $ this-> iConnectId is an active database connection, issued in the format of a multifunctional object.
The MySQL fetch array tool is a simple and efficient tool, but its use is conveniently performed in an object-oriented manner.
Database Access Object
MySQL is an extremely simple and effective tool for organizing information. MySQL databases for all the time of their use have proven to be a reliable and effective tool for storing and accessing data.
The mechanism for recording database queries and their application is simple, but in practice it is ideal to format it in the format of an entire database object, a separate table, or a specific data structure.
The design of MySQL fetch in real practice is rather cumbersome, especially when it is necessary to select information from one table of basic information, for example, staffing, and then select data from other tables for employees:
- work history;
- biographical data;
- production achievements;
- area of professional competence;
- other data.
As a rule, these data will not be filled in for all employees, and besides, if you enter particulars for each line in the staff table, this will make it cumbersome and inconvenient to use.
Database Access Object Level
The scope of MySQL fetch is to transform data into an array. This can be done at the method level of the database object. In this case, the developer creates for himself a high-level tool that provides access to any database table in the same way.
A call is made to the method, which immediately forms the desired data array, and the operations of generating the request, its execution, and processing the result lie in the body of this method.
This is very convenient, and the need to write bulky constructions, creating a precedent for a possible error, disappears. It is especially important: this approach simplifies probable modifications to the database structure, since they do not go beyond the boundaries of the database access object. If necessary, it will be necessary to redo only the object of access to the database, and not all the functionality of the site.
It is even more convenient to add real-purpose objects over the object of access to the database. For example, a full-time description that has the following properties: a list, output of employee data, changing an employee’s card, etc. Data access tools are hidden in the bowels of methods of real objects, which simplifies the work of the developer and improves the quality of the result.