Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Łukasz Czuliński
Łukasz Czuliński
8,646 Points

$row variable. Not sure what it is.

So I'm learning MySQL and came across the $row variable. I'm unclear what it is and cannot find a clear definition after searching online.

I was following along in W3School's tutorial. Here's the code:

<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM Persons");

while($row = mysqli_fetch_array($result)) {
  echo $row['FirstName'] . " " . $row['LastName'];
  echo "<br>";
}

mysqli_close($con);
?>

$row was not defined anywhere in the code and the documentation's only reference to it was: 'To print the value of each row, we use the PHP $row variable ($row['FirstName'] and $row['LastName']).'

How is it actually declared?

3 Answers

Hello,

It is declared here:

while($row = mysqli_fetch_array($result))

Is is declared and then using one = not two (two are for compare) it gets the value returned from mysqli_fetch_array($result) function. And this function returns an array. That's why $row is then used as array: $row['FirstName'] and the id of this array is defined as table row in a database.

Hello,

It is declared here:

while($row = mysqli_fetch_array($result))

Is is declared and then using one = not two (two are for compare) it gets the value returned from mysqli_fetch_array($result) function. And this function returns an array. That's why $row is then used as array: $row['FirstName'] and the id of this array is defined as table row in a database.

Łukasz Czuliński
Łukasz Czuliński
8,646 Points

Wow...facepalm. I can't believe I missed that. I'm a PHP noob and not used to not having the var ____ =

Mersi mnogo!