I don't understand what this does $row[ ]

I have seen this code and have even got some of it to work, but I do not understand what it does.

				if($_SESSION['token_id'] == $row[0]) {
					echo ", {$_SESSION['first_name']}!";
					$loggedin = 1;
					}
					else {
					echo ", please login.";
					$loggedin = 0;
					}

In this example there is $row[0] but in other parts of the code there are different numbers. What is the significance of the number between the [ and the ] ?

I have spent the last several hours reading the online PHP and MySQL manuals without finding the information.

You want print_r to return the data, not echo it (your echo statement already does that), so you need to put true as the second parameter:


echo '<pre>', print_r($row, true), '</pre>';

:slight_smile:

I tried the
echo ‘<pre>’,print_r($data),‘</pre>’;
and all I got was 1.

I then tried
echo ‘<pre>’,print_r($row),‘</pre>’;
and then I got:
Array
(
[0] => 1018860
)
1

I don’t know what all that means, but by setting my code to $row[0] I got it working.

I am guessing that the only item in the array is [0] => 1018860 and the final item is a 1 indicating there is only one value in the array.

Am I close with my guess?

echo ‘<pre>’,print_r($data),‘</pre>’;

Depends on how you added it to the array.

How can I read the array?

Can I
echo $row;
to see what is in the array?

So I could save myself a lot of trouble if I made my code read:

if($_SESSION['token_id'] == $row['token_id"]) {
                    echo ", {$_SESSION['first_name']}!";
                    $loggedin = 1;
                    }

But as part of my learning:
In a previous script, I selected 5 items from the MySQL database and then added the token_id to the array. Would this make token_id be at $row[5]?

Don’t ever use these numeric indices unless you like debugging sessions that last for several hours.

fetch_array without qualifier flags returns both numerical and associative entries.

Though it is more efficient to use assoc, or flag the fetch_array to return only assoc.

I am assuming $row was created from something like

$row = mysql_fetch_array($result);

where $result is the output result set from a database query.

If so, then say each row of data in $result contains 3 columns of data - firstName, lastName, emailAddress.

$row[0] will refer to the first column’s data - firstName

$row[1] will refer to the secon column’s data - lastName

and so on.

Instead of using an indexed array you could use mysql_fetch_assoc() instead which returns an associative array for $row. In this case you can refer to the column names by their actual returned names in $result.

So the value for firstName in a row could be referenced with $row[‘firstName’]

$row is an array - it can therefore contain many different values all at the same time with one value stored in the first position $row[0], a different value in the second position $row[1] and so on for as many values as $rows stores.

While putting this in various places in my code makes the page look strange, it does seem to be helping me understand what is happening and …

I think I am close to finding out why my script is not working.