Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding fetch_assoc()

Tags:

methods

php

I am trying to understand how/why fetch_assoc works the way it does. I have the following piece of code:

$results = $connectToDb->fetch("SELECT * FROM customer");
$resultsArray = $results->fetch_assoc();
print_r($resultsArray);  //print_r 1

while($row = $results->fetch_assoc()){
    print_r($row);       //print_r 2
}

The query returns 3 rows from a table. Why does the 1st print_r return only the 1st row of the queried data but the 2nd print_r returns all 3? How does putting fetch_assoc into a while loop tell it to do the action more than once? I read that fetch_assoc returns either an associative array or NULL but I'm struggling to understand how the while loop "tells" fetch_assoc to fetch the next row, if that makes sense?

Thank you.

like image 671
Mouse Avatar asked Sep 01 '16 09:09

Mouse


People also ask

What is the use of Fetch_assoc () in PHP?

Definition and Usage The fetch_assoc() / mysqli_fetch_assoc() function fetches a result row as an associative array. Note: Fieldnames returned from this function are case-sensitive.

What is the difference between Fetch_assoc and Fetch_array?

fetch_array returns value with indexing. But Fetch_assoc just returns the the value. here array location 0 contains 11 also this location name is 'id'. means just returns the value.

How does mysql_fetch_assoc work?

The mysql_fetch_assoc() function returns a row from a recordset as an associative array. This function gets a row from the mysql_query() function and returns an array on success, or FALSE on failure or when there are no more rows.

What will mysql_fetch_assoc () return?

Returns an associative array that corresponds to the fetched row and moves the internal data pointer ahead. mysql_fetch_assoc() is equivalent to calling mysql_fetch_array() with MYSQL_ASSOC for the optional second parameter. It only returns an associative array.


2 Answers

Lets try to understand your code and how it works:

$results = $connectToDb->fetch("SELECT * FROM customer");

A variable $results has a collection of rows which are returned by a query. The size of the collection can be from 0 to n.

$resultsArray = $results->fetch_assoc();

This line fetch the first element from the collection. If the collection is empty it will return NULL.

while($row = $results->fetch_assoc()){
}

It can be decoupled in the following steps:

  1. Calculate $row = $results->fetch_assoc() and return array with elements or NULL.
  2. Substitute $row = $results->fetch_assoc() in while with gotten value and get the following statements: while(array(with elements)) or while(NULL).
  3. If it's while(array(with elements)) it resolves the while condition in True and allow to perform an iteration.
  4. If it's while(NULL) it resolves the while condition in False and exits the loop.
like image 113
Andrej Ludinovskov Avatar answered Sep 18 '22 20:09

Andrej Ludinovskov


I think it useful:

function get_posts(){
$posts = array();
if ($result = $mysqli->query("SELECT  id, post_userid, name, lang, country, post_image, post_date, post_date_updated FROM posts ORDER BY `post_date` DESC ;")) {

    while ($row = $result->fetch_assoc())
    {
        $posts[$row['id']] = $row ;   
    }
} else {
    printf("Prepared Statement Error: %s\n", $mysqli->error);
}
return $posts;

}

like image 33
norullah karimi Avatar answered Sep 21 '22 20:09

norullah karimi