$row[0] in Controller, Chapter 9

I am on page 308 working my way through the index.php file in the authors directory, and I am having trouble figuring out why “[0]” follows $row in the while loop. It seems as if it doesn’t need to be their.

// For each joke
while ($row = mysqli_fetch_array($result))
{
$jokeId = $row[0];

	// Delete joke category entries
	$sql = "DELETE FROM jokecategory WHERE jokeid='$jokeId'";
	if (!mysqli_query($link, $sql))
	{
		$error = 'Error deleting category entries for joke.';
		include 'error.html.php';
		exit();
	}
}

Have you tried taking it out and running the code?

Yes and I got some strange results. Some data was removed from the tables while other data, which should have been removed, remained. I couldn’t put my finger on what was happening though. I am pretty sure it is necessary, but I cannot figure out how the [0] is making this happen.

Thanks for the help.

I guess to understand things you need to follow the code back a bit.

	// Get jokes belonging to author
	$sql = "SELECT id FROM joke WHERE authorid='$id'";
	$result = mysqli_query($link, $sql);
	if (!$result)
	{
		$error = 'Error getting list of jokes to delete.';
		include 'error.html.php';
		exit();
	}

	// For each joke
	while ($row = mysqli_fetch_array($result))
	{
		$jokeId = $row[0];

		// Delete joke category entries
		$sql = "DELETE FROM jokecategory WHERE jokeid='$jokeId'";
		if (!mysqli_query($link, $sql))
		{
			$error = 'Error deleting category entries for joke.';
			include 'error.html.php';
			exit();
		}
	}

The SELECT query gets the id of all rows where the authorid value equals the value of the $id variable if any, and returns an array of single key=>value arrays (because the SELECT expression only had 1 field. ‘id’ ‘date’ would be 2 etc.) eg.
0/‘id’ => 123
1/‘id’ => 456
2/‘id’ => 789

mysqli_fetch_array() uses the numeric keys rather the the associative and puts them into $row array. Because the SQL returned arrays only have a single key=>value, ‘0’ specifies the first (and in this case only) key

the first pass through the while{},
$jokeId = $row[0];
$jokeId = 123;
second pass
$jokeId = 456;
etc.

If you leave off the “[0]” it would be
$jokeId = array; // the “type”

Then that value is used in the DELETE query
$sql = “DELETE FROM jokecategory WHERE jokeid=‘123’”;
$sql = “DELETE FROM jokecategory WHERE jokeid=‘array’”;

TBH, I’m suprised it isn’t throwing errors rather than gving unexpected results.

Any help or more confused?

It is helpful, thank you. I am still a little confused about the last part:

[COLOR=“Blue”]If you leave off the “[0]” it would be
$jokeId = array; // the “type”

Then that value is used in the DELETE query
$sql = “DELETE FROM jokecategory WHERE jokeid=‘123’”;
$sql = “DELETE FROM jokecategory WHERE jokeid=‘array’”;[/COLOR]

Are you saying the DELETE query would delete jokeid ‘123’ then attempt to delete jokeid ‘array’ all in one step if [0] was left off? Or are you saying the DELETE query would delete jokeid ‘123’ if [0] was included, but attempt to delete jokeid ‘array’ if [0] was left out?

I am new to PHP so I may seem dense about some obvious things. I read through the PHP.net/manual page on arrays but I am still not putting it all together. I appreciate all your help. Thank you.

I didn’t mean it to look sequential, but alternative.

$row[0] has a value, but $row is a data type.

That’s where I get confused. Unless there was actually a row with a jokeid value of ‘array’ (I’m guessing it’s probably an INT so that would be imposible) AFAIK it would throw an error and not do anything.

I find the fact that it is doing something, and unexpected at that, a bit disconcerting. All the more reason to be careful writing code.

Okay, I think I am understanding you now. I ran through all the code examples in the book and realized that every time $row is used, a specific referenced is given, e.g. $row[‘joketext’]. I did not realize this before.

Also, just to be sure I ran another test. I ran a SELECT * query on all tables and cut and pasted the results into Word. I worked my way through the “Delete” portion of the index.php file to identify and highlight all data to be changed under normal circumstances (i.e. with [0]) if a certain author was deleted. I then duplicated the index.php file right out of the code archive that came with the book. I edited the index.php file to remove the “[0]” only. I then opened the edited index.php file in my browser and deleted the above author. After this I ran SELECT * queries on all tables and compared the results. All data was removed as expected except the appropriate entries in the “jokecategory” table. No errors were given.

I really appreciate your help on this. Thank You!