In this article, we will focus on the mysqli_fetch_array()
function in PHP, which is used to fetch a row from a MySQLi result set as an associative or numeric array. We will provide you with an overview of the function, how it works, and examples of its use.
Introduction to the mysqli_fetch_array() function
The mysqli_fetch_array()
function is a built-in function in PHP that is used to fetch a row from a MySQLi result set as an associative or numeric array. This function is useful when you need to fetch a single row from a MySQLi query and store it in an array for further processing.
How to use the mysqli_fetch_array() function
Using the mysqli_fetch_array()
function is very simple. You just need to call the function on a valid MySQLi result set. Here is an example:
<?php
$mysqli = mysqli_connect("localhost", "username", "password", "database");
$query = "SELECT * FROM my_table WHERE id = 1";
$result = mysqli_query($mysqli, $query);
if ($result) {
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
echo $row['column1'] . " - " . $row['column2'];
}
mysqli_close($mysqli);
?>
In this example, we call the mysqli_connect()
function to connect to a MySQL database with a username and password. We then execute a query using the mysqli_query()
function with a WHERE
clause to select a single row from the table. We store the result in a variable and check if there was a result using the $result
variable. If there was a result, we call the mysqli_fetch_array()
function to fetch a row from the result set as an associative array. We then output the values of two columns for the row.
Advanced usage
The mysqli_fetch_array()
function can also be used in more advanced scenarios. For example, you can use the function to fetch a row as a numeric array instead of an associative array. Here is an example:
<?php
$mysqli = mysqli_connect("localhost", "username", "password", "database");
$query = "SELECT * FROM my_table WHERE id = 1";
$result = mysqli_query($mysqli, $query);
if ($result) {
$row = mysqli_fetch_array($result, MYSQLI_NUM);
echo $row[0] . " - " . $row[1];
}
mysqli_close($mysqli);
?>
In this example, we call the mysqli_connect()
function to connect to a MySQL database with a username and password. We then execute a query using the mysqli_query()
function with a WHERE
clause to select a single row from the table. We store the result in a variable and check if there was a result using the $result
variable. If there was a result, we call the mysqli_fetch_array()
function to fetch a row from the result set as a numeric array. We then output the values of two columns for the row.
Conclusion
In conclusion, the mysqli_fetch_array()
function is a useful tool for fetching a single row from a MySQLi result set and storing it in an array for further processing. By understanding how to use the function and its advanced usage scenarios, you can take advantage of this feature to create powerful and flexible MySQLi queries.
Practice Your Knowledge
Quiz Time: Test Your Skills!
Ready to challenge what you've learned? Dive into our interactive quizzes for a deeper understanding and a fun way to reinforce your knowledge.