I have small PHP script which has
$query = "SELECT MAX(id) FROM `dbs`";
//query run
$row = mysql_fetch_array($result);
$val = $row[0];
Which runs fine, but I want to understand why i can’t access the row with the fieldname, like if i have this
$query = "SELECT id FROM `dbs`";
i am able to use the folowing
$val = $row['id'];
but whenever i use this MAX() function, i have to change to
$val = $row[0];
to access the values
I have no clue about this. Any help would be appreciated. Thankss
You need to give it an alias:
Edit:
To explain this it’s probably best to show an example of a different query:
Using the above it will return as many rows are in the table, with 2 columns –
idandmaxId(althoughmaxIdwill be the same in each row due to the nature of the function).Without giving it an alias MYSQL doesn’t know what to call it, so it won’t have an associative name given to it when you return the results.
Hope that helps to explain it.