Home >Database >Mysql Tutorial >Why Does My MySQL `mysql_fetch_array` Only Return One Row?
MySQL Fetch Array Returns Only One Row
In the given code, the mysql_query function executes a query that retrieves rows from the directory table where specific conditions are met. However, the issue arises when attempting to access individual elements of the returned rows using mysql_fetch_array.
The key point to understand is that mysql_fetch_array returns a single row from the result set. The initial call to mysql_fetch_array retrieves the first row, and subsequent calls would return the next rows one by one.
To access all the rows returned by the query, you need to use a loop to repeatedly call mysql_fetch_array. Here's an example that will iteratively fetch and display each row's artist column:
$array = mysql_query("SELECT artist FROM directory WHERE artist LIKE 'a%' OR artist LIKE 'b%' OR artist LIKE 'c%'"); while ($data = mysql_fetch_array($array)) { echo $data['artist']; // Access each row's artist value }
By using a loop, you will iterate through all the rows and access each element within them.
The above is the detailed content of Why Does My MySQL `mysql_fetch_array` Only Return One Row?. For more information, please follow other related articles on the PHP Chinese website!