I’m getting this column in this bd
$result = mysql_query("SELECT short FROM textos");
and I’m trying to echo only one of the results based on the array it returns:
$col = mysql_fetch_assoc($result);
echo "<b>Short:</b>".$col[1]."<br/>";
apparently this $col array can’t be accessed this way. How should it be done? Thanks
That has been already stated in comments above, so just a bit of explanation here.
mysql_fetch_assocretrieves a result row for you presented as an associative array (an array where keys are field names and values are field values). Your query returns only one field (which isshort), but still it doesn’t make your row a single scalar value – it remains an array, only with a single element.So you need to refer it as
$row['short'], or in your sample$col['short'].Bear in mind that query might return no results – you can learn that by checking if the returned value is not an array but scalar
falseinstead, e.g.Putting
LIMITinto your query like again comments suggest is a good idea as well because you wouldn’t be returning potentially huge amount of data when you only need one row actually. The result would still come as a multi-dimensional array though, so that part won’t change.