0

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

3
  • 3
    $col[0]['short'] and you can add LIMIT 1 to the query + you should check if the result array is set. Commented Nov 17, 2012 at 22:09
  • 1
    Looks like you need mysql_fetch_array instead: php.net/manual/en/function.mysql-fetch-array.php, and the fist index is 0 rather than 1 Commented Nov 17, 2012 at 22:10
  • 3
    Basic debugging is in order here, and would have revealed the answer instantly. When you don't get the value you expect to get, or have undefined array keys, check the structure with var_dump($col) Commented Nov 17, 2012 at 22:14

3 Answers 3

1

That has been already stated in comments above, so just a bit of explanation here.

mysql_fetch_assoc retrieves 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 is short), 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 false instead, e.g.

if ($col === false) {
    echo 'Error occured<br/>';
} else {
    echo "<b>Short:</b>".$col['short']."<br/>";
}

Putting LIMIT into 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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks!! great explanation. But I ended up getting an error: undefined offet: 0. I ran var_dump and I got the text I wanted in the array, but to echo it i get this problem. Do you know why is that?
uhm? i might getting old but iirc mysql_fetch_assoc gets a row (with associative indexes), not an array of rows.
iRas, yes, you're absolutely right! I'm now ashamed that the answer has been accepted. Will edit it now.
Andre, I have edited my answer - sorry, I read the function name wrong and provided an explanation for the wrong one. Please take a look again.
1

To access the first element use $col[0]['short'].
If you only want to output one element anyways you can add LIMIT 1 to the MySQL query.
After querying you should check if the result array is set otherwise php will throw an error saying that $col[0]['short'] is not set.

Comments

1

There are three mysql_fetch functions which getting rows:

  • mysql_fetch_array() Fetch an array with both indexes (numeric and associative)
  • mysql_fetch_num() Fetch an array with numeric indexes
  • mysql_fetch_assoc() Fetch an array with associative indexes

In your example you will get an array that is looking like this one for the function mysql_fetch_array():

array(2) {
  [0]=>
  string(3) "foo"
  ["short"]=>
  string(3) "foo"
}
$statement = 'SELECT short FROM textos';
$result = mysql_result($statement);
if ($result) {
    while ($row = mysql_fetch_assoc($result)) {
        var_dump($row); // or echo "<b>Short:</b>".$row['short']."<br/>"; or something else you like to do with rows of the above statement
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.