Question:
In how many ways we can retrieve the data in the result set of MYSQL using PHP?
snehashish c
2008-05-17 12:31:41 UTC
In how many ways we can retrieve the data in the result set of MYSQL using PHP?
Four answers:
NC
2008-05-18 19:43:01 UTC
Depends on what extension (mysql or mysqli) you are using. With mysql, there are at least five relevant functions:



mysql_fetch_array — Fetch a result row as an associative array, a numeric array, or both

mysql_fetch_assoc — Fetch a result row as an associative array

mysql_fetch_field — Get column information from a result and return as an object

mysql_fetch_object — Fetch a result row as an object

mysql_fetch_row — Get a result row as an enumerated array



With mysqli, there are even more:



mysqli_stmt::fetch — Fetch results from a prepared statement into the bound variables

mysqli_result::fetch_array — Fetch a result row as an associative, a numeric array, or both

mysqli_result::fetch_assoc — Fetch a result row as an associative array

mysqli_result::fetch_field_direct — Fetch meta-data for a single field

mysqli_result::fetch_field — Returns the next field in the result set

mysqli_result::fetch_fields — Returns an array of objects representing the fields in a result set

mysqli_result::fetch_object — Returns the current row of a result set as an object

mysqli_result::fetch_row — Get a result row as an enumerated array
2008-05-20 03:39:00 UTC
functions for returning entire rows of data:

mysql_fetch_array()

mysql_fetch_row()

mysql_fetch_assoc()

mysql_fetch_object()



from the php manual:

array mysql_fetch_array ( resource $result [, int $result_type ] )

meaning it returns an array,



usage would be like so:

$result = mysql_query("SELECT something, andthis FROM atable");

$row = mysql_fetch_array( $result );



$row would be

Array(

0 => 'somethings value',

'something' => 'somethings value',

1 => 'andthiss value',

'andthis' => 'andthiss value'

)



yes there would be 2, let me say that in english... TWO copies of every row, wasting alot of memory. This is why it is always important to specify result type (the second argument for mysql_fetch_array)

The 3 types are: MYSQL_BOTH, MYSQL_ASSOC, and MYSQL_NUM

MYSQL_BOTH is default when none else is specified and it means to return as both numerated and associative keys, never use this.

MYSQL_NUM returns a numerated array meaning it is an arrays which keys are incremented from 0

MYSQL_ASSOC returns an associative array meaning its keys will be the names of the columns selected.



mysql_fetch_assoc($result) is just like mysql_fetch_array($result, MYSQL_ASSOC)

mysql_fetch_row($result) is just like mysql_fetch_array($result, MYSQL_NUM)



mysql_fetch_object($result [, class name [, params]] ) returns an object, i dont have experience using this but more can be read at http://php.net/mysql_fetch_object





You can also get data from a mysql result set with mysql_result()

usage is mysql_result($result, $row, $column)

if you only SELECT one column, you can omit it in the function call.

$row is so you can jump to a row other than the first row returned. The first row is 0.

example:

$result = mysql_query("SELECT COUNT(*) FROM sometable");

$rowcount = mysql_result($result, 0);
2016-04-15 15:59:43 UTC
You can do it by 4 Ways 1. mysql_fetch_row. 2. mysql_fetch_array 3. mysql_fetch_object 4. mysql_fetch_assoc Refer following link



For the best answers, search on this site https://smarturl.im/aDET2
2014-10-06 22:06:20 UTC
I'm making good profit with a binary option signal software called "autobinary signal". It's great!! Check here for more information ( http://forexsignal.kyma.info )


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...