Hello:

I have a very general question regarding mysql_result function.

I was reading a PHP/MySQL book and the example code after running a query used this syntax:

$item_price=mysql_result($get_item_res,0,'item_price');

Can someone tell me how is this different from using: mysql_fetch_array or mysql_fetch_row?

Which method is better and more efficient?

Thank you for your explanations and input.

    focus310 wrote:

    Can someone tell me how is this different from using: mysql_fetch_array or mysql_fetch_row?

    Well, if you look at the manual page for [man]mysql_result/man, you'll see that it returns 1 column from 1 row. If you look at the manual for [man]mysql_fetch_array/man or [man]myql_fetch_row/man, you'll see they return arrays.

    focus310 wrote:

    Which method is better and more efficient?

    Depends on what you're trying to do, such as only working with one result from one row versus working with multiple columns from a row at a time.

      From the Manual

      mysql_result

      (PHP 4, PHP 5)

      mysql_result — Get result data
      Description
      string mysql_result ( resource $result, int $row [, mixed $field] )

      Retrieves the contents of one cell from a MySQL result set.

      When working on large result sets, you should consider using one of the functions that fetch an entire row (specified below). As these functions return the contents of multiple cells in one function call, they're MUCH quicker than mysql_result(). Also, note that specifying a numeric offset for the field argument is much quicker than specifying a fieldname or tablename.fieldname argument.
      Parameters

      result

      The result resource that is being evaluated. This result comes from a call to mysql_query().

      row

      The row number from the result that's being retrieved. Row numbers start at 0. 

      field

      The name or offset of the field being retrieved.
      
      It can be the field's offset, the field's name, or the field's table dot field name (tablename.fieldname). If the column name has been aliased ('select foo as bar from...'), use the alias instead of the column name. If undefined, the first field is retrieved. 

      Return Values

      The contents of one cell from a MySQL result set on success, or FALSE on failure.

        Thank you. I think I know what I need to do.

          Write a Reply...