ArticleslgStudy

science

MySQLi

MySQLi is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand MySQLi rather than just read about it. In short: The MySQLi Extension (MySQL Improved) is a relational database driver used in the PHP scripting language to provide an interface with MySQL protocol compatible databases (MariaDB, MySQL, Percona Server, TiDB). MySQLi is an improved version of the older PHP MySQL driver, offering various benefits.

Key takeaways

  • MySQLi belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect MySQLi to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of MySQLi from memory before moving on to harder problems.

Reference excerpt

The MySQLi Extension (MySQL Improved) is a relational database driver used in the PHP scripting language to provide an interface with MySQL protocol compatible databases (MariaDB, MySQL, Percona Server, TiDB). MySQLi is an improved version of the older PHP MySQL driver, offering various benefits. The authors of the PHP scripting language recommend using MySQLi when dealing with MySQL server versions 4.1.3 and newer, since it takes advantage of new functionality.

Technical details The PHP code consists of a core, with optional extensions to the core functionality. PHP's MySQL-related extensions, such as the MySQLi extension, and the MySQL extension, are implemented using the PHP extension framework. The MySQLi extension provides various benefits with respect to its predecessor, the most prominent of which (according to the PHP website) are:

An object-oriented interface Support for prepared statements Support for multiple statements Support for transactions Enhanced debugging support

Comparison of features Source: There are three main API options when considering connecting to a MySQL database server:

PHP's MySQLi Extension PHP Data Objects (PDO) PHP's MySQL Extension

Usage Source:

Dual interface The MySQLi extension features a dual interface: it supports both the procedural and object-oriented programming paradigms. Users migrating from the old MySQL extension may prefer the familiarity of the procedural interface; in many cases, the function names differ only by prefix. Some MySQLi functions take a connection handle as their first argument, whereas matching functions in the old MySQL interface took it as an optional last argument. New and old native function calls

Connections The MySQL server supports the use of various transport layers for connections. Connections use TCP/IP, Unix domain sockets or Windows named pipes. The hostname localhost has a special meaning: it is bound to the use of Unix domain sockets. It is not possible to open a TCP/IP connection using the hostname localhost – instead 127.0.0.1 must be used.

Connection examples: Output

Localhost via UNIX socket 127.0.0.1 via TCP/IP

Executing statements Statements can be executed with the mysqli_query(), mysqli_real_query() and mysqli_multi_query() functions. The mysqli_query() function is the most common, and combines the executing statement with a buffered fetch of its result set, if any, in one call. Calling mysqli_query() is identical to calling mysqli_real_query() followed by mysqli_store_result(). Example:

Buffered result sets After statement execution, results can be retrieved at once to be buffered by the client or by read row by row. Client-side result set buffering allows the server to free resources associated with the statement results as early as possible. Generally speaking, clients are slow consuming result sets. Therefore, it is recommended to use buffered result sets. mysqli_query() combines statement execution and result set buffering. PHP applications can navigate freely through buffered results. Navigation is fast because the result sets are held in client memory. Example: Navigation through buffered results

The above example will output:

Reverse order... id = 3 id = 2 id = 1 Result set order... id = 1 id = 2 id = 3

Unbuffered result sets If client memory is a short resource and freeing server resources as early as possible to keep server load low is not needed, unbuffered results can be used. Scrolling through unbuffered results is not possible before all rows have been read. Example: Navigation through unbuffered results

Result set values data types The mysqli_query(), mysqli_real_query() and mysqli_multi_query() functions are used to execute non-prepared statements. At the level of the MySQL Client Server Protocol, the command COM_QUERY and the text protocol are used for statement execution. With the text protocol, the MySQL server converts all data of a result sets into strings before sending. This conversion is done regardless of the SQL result set column data type. The MySQL client libraries receive all column values as strings. No further client-side casting is done to convert columns back to their native types. Instead, all values are provided as PHP strings. Example: Text protocol returns strings by default

The above example will output:

id = 1 (string) label = a (string)

It is possible to convert integer and float columns back to PHP numbers by setting the MYSQLI_OPT_INT_AND_FLOAT_NATIVE connection option, if using the mysqlnd library. If set, the mysqlnd library will check the result set meta data column types and convert numeric SQL columns to PHP numbers, if the PHP data type value range allows for it. This way, for example, SQL INT columns are returned as integers. Example: Native data types with mysqlnd and connection option

The above example will output:

id = 1 (integer) label = a (string)

Prepared statements The MySQL database supports prepared statements. A prepared statement or a parameterized statement is used to execute the same statement repeatedly with high efficiency. The prepared statement execution consists of two stages: prepare and execute. At the prepare stage a statement template is sent to the database server. The server performs a syntax check and initializes server internal resources for later use. The MySQL server supports using anonymous, positional placeholders with ?.

Stored procedures The MySQL database supports stored procedures. A stored procedure is a subroutine stored in the database catalog. Applications can call and execute the stored procedure. The CALL SQL statement is used to execute a stored procedure. Stored procedures can have IN, INOUT and OUT parameters, depending on the MySQL version. The MySQLi interface has no special notion for the different kinds of parameters. Input parameters are provided with the CALL statement.

Multiple statements MySQL optionally allows having multiple statements in one statement string. Sending multiple statements at once reduces client-server round trips but requires special handling. Multiple statements or multi queries must be executed with mysqli_multi_query(). The individual statements of the statement string are separated by semicolons. Then, all result sets returned by the executed statements must be fetched. The MySQL server allows having statements that do return result sets and statements that do not return result sets in one multiple statement.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with MySQLi

Start with the simplest possible case. Write down what MySQLi claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to MySQLi before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about MySQLi ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of MySQLi

In research
MySQLi appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses MySQLi in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
MySQLi is common in secondary-school and first-year university syllabi. It links to neighbouring topics Data access technologies, MySQL, PHP libraries, so understanding it makes those chapters shorter.
In everyday life
Look for MySQLi outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “MySQLi” →

How to study MySQLi in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what MySQLi means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain MySQLi out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is MySQLi in simple terms?

The MySQLi Extension (MySQL Improved) is a relational database driver used in the PHP scripting language to provide an interface with MySQL protocol compatible databases (MariaDB, MySQL, Percona Server, TiDB). MySQLi is an improved version of the older PHP MySQL driver, offering various benefits.

Why does MySQLi matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study MySQLi?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on MySQLi.

Tags

  • Data access technologies
  • MySQL
  • PHP libraries
  • SQL data access

Keep exploring