The Missing mysql_result() in MySQLi
Porting legacy PHP applications from the deprecated mysql_* extension to MySQLi often reveals a specific friction point: the lack of a direct function-to-property equivalent for accessing single values. In the old world, mysql_result($result, 0, 'blah') allowed developers to grab a scalar value without explicitly fetching the entire row into an associative array first. This was convenient for queries expected to return exactly one row and one column.
When migrating to MySQLi, that convenience disappears immediately. The API design shifted from procedural functions to object-oriented properties and methods. You cannot call $result->result(0, 'blah'). There is no magic shortcut in the core MySQLi extension that mirrors this specific behavior while maintaining the same internal logic of "peeking" at a single column index without materializing the row object.
The immediate answer is that you must embrace the fetch_assoc() method. The migration path looks like this:
if ($r && $r->num_rows > 0) {
$row = $r->fetch_assoc();
$blarg = $row['blah'];
}
This pattern is verbose compared to the four-line condensation mysql_result() offered, but it aligns with the fundamental architectural shift of MySQLi. The old extension treated the result set as a stream where you could poke through without consuming the underlying data structure in memory. MySQLi, conversely, encourages treating the result object as a collection of rows that are accessed sequentially.
Why the API Changed
The removal of mysql_result() isn't arbitrary; it reflects a move away from procedural shortcuts toward more explicit state management. In the old API, calling mysql_result() on a cursor effectively told the database driver to fetch the current row's data for that specific column and discard the rest if not needed. This was efficient for single-row queries but discouraged for anything involving multiple rows, leading to inconsistent performance characteristics.
MySQLi enforces a stricter contract: you either iterate over all rows or you stop after fetching one. By requiring fetch_assoc(), the API forces developers to explicitly acknowledge that they are materializing a row object into memory. This makes the code more predictable and easier to debug, as the state of the result set cursor is always synchronized with the data held in memory.
The Performance Trade-off
You mentioned that mysql_result() was slower when working with multiple rows. That observation is accurate in the context of the old extension, but the reason isn't simply overhead; it's about how the driver handled the cursor position. When you called mysql_result(), the driver had to ensure the internal pointer remained valid while potentially only reading a subset of the requested data.
In MySQLi, fetch_assoc() is optimized for the common case of iterating through results or retrieving a single row at a time. If your query is designed to return exactly one row, the performance difference between using mysql_result() and fetching an associative array is negligible in modern PHP runtimes. The overhead of creating the array structure is minimal compared to the network latency and database parsing time required to execute the query.
However, if you are processing thousands of rows in a loop, avoiding fetch_assoc() in favor of fetch_row() (which returns numeric arrays) can save memory allocations if your application does not need named columns. But for the single-row use case found in legacy code, the readability and safety of fetch_assoc() far outweigh the marginal cost of array creation.
Modern Alternatives with PDO and Laravel
If you are in a position to refactor beyond just MySQLi, consider using PDO or a modern framework like Laravel. With PDO, you can access values similarly without fetching the whole row if you bind parameters correctly, though the standard practice remains consistent.
In a Laravel context, which is often built on top of these database drivers, developers rarely deal with raw fetch_assoc() calls for single-value lookups. Instead, Eloquent ORM handles this abstraction. If you are looking to port code that relies heavily on mysql_result() style shortcuts, refactoring those specific blocks into clean array access within a repository pattern or using Eloquent's first()->blah approach is often the most robust long-term strategy.
For now, if you must stay with MySQLi, accept the extra line of code. It represents a more robust understanding of how database cursors work in modern PHP ecosystems. The slight verbosity ensures your code remains clear and avoids the pitfalls of implicit cursor consumption that plagued the old mysql_* extension.