1

i had a headache over the last few days trying to understand this snippet of code. it's about retrieving data from a database table nothing hard (i'm using mysql) . but i'm trying to understand the code. here is the code:

<?php
include 'PDOconnect.php';
//Query
$result = $connection->query('SELECT * FROM video_games');
//Fetch
$data = $result->Fetch();

while ($data = $result->Fetch()) {
echo $data['name']."<br />";
}

?>

first let me explain, the second line is including the connection code to the database i'm using the PDO way of connecting. the connection is fine . my table is called video_games and it had a column called 'name'. and i'm trying with this code to retrieve all the data from the column 'name'.

1- so what i want to understand is what is the $result variable (line 6) , i've heard it's a Resource. what a resource in mysql means, and what's inside of the variable $result is it the whole table or what exactly ??

2- what the function fetch() does ?? it's confusing .

3- what i know from studying the basic syntax of php is that inside the while condition the value must be true in order to execute the code inside. but here there is ($data = $result->Fetch()) .

4- is the fetch() method automatically incremented ?? i mean why it is working successfully inside the while condition, so it must be incrementing over and over again ??

please help my mind is blowing right now.

2 Answers2

1

$result is not a Resource,. it's a PDOStatement. see the docs: http://www.php.net/manual/en/pdo.query.php

Once a PDOStatement is executed (automaticalyl using ->query()) the statement holds the result the database returned.

Everytime you ->fetch() it returns the current row the Statement is pointing to. After that it points it pointer to the next row (so yes, 'automatically incremented).

Now, your code:

Everything inside if and while statements is evaluated using loose comparision (==)

if ( $a ) actually checks or $a == true.

$data = $result->fetch() simply sets a value to $data. Then the while checks or $data == true. IF so, it does what it has to do. (see php == vs === operator for more about comparisons in php)

Now, a little remark on your code: the first row is not outputted since you don't do anything with the first fetched result. Simply remove

//Fetch
$data = $result->Fetch();

So your code would become:

<?php
include 'PDOconnect.php';
//Query
$result = $connection->query('SELECT * FROM video_games');

while ($data = $result->Fetch()) {
echo $data['name']."<br />";
}

A good tutorial about PDO: http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/

Community
  • 1
  • 1
Pinoniq
  • 1,365
  • 9
  • 13
1

1 what is the $result variable

It is not a resource. It's actually an object of PDOStatement class

2 what the function fetch() does ?? it's confusing.

A manual page is always at your service. Just type PDO fetch in the browser's address bar and click the first link opened. It is extremely easy and no less powerful.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345