The functions are all very similar:
mysql_fetch_array(), mysql_fetch_assoc(), mysql_fetch_object()
I have recently started using mysql_fetch_object as I am doing alot more OOP with PHP.
But what are peoples opinions on which one is best to use and why, and maybe which scenario they are best to be used in.
Thanks for your thoughts!
mysql_fetch_array will get you an array that can have as keys :
both numbers and names of columns, if using MYSQL_BOTH
columns names, using MYSQL_ASSOC -- in this case, you'll get the same thing you get when using mysql_fetch_assoc
only numbers (depending on the order of columns in the query), if using MYSQL_NUM
Getting results indexed by columns names is probably the most useful solution -- easier to use, at least.
But getting results indexed by the positions of the fields in the select clause is interesting in one situtation : when you have several columns that have the same name or alias.
In this case, as you cannot have two entries with the same index in an array, you will be able to access only one of those columns using the column name as index.
For the other columns that have the same name, you'll have to use numeric indexes.
That situation is probably the only case for which I would use mysql_fetch_array -- and I rather prefer using aliases in my query, to avoid that situation -- it's more clear, in my opinion.
mysql_fetch_assoc will get you an array, with columns names as keys, and data as values.
Not much to say, actually.
And mysql_fetch_object will get you objetcs in return.
Choosing between mysql_fetch_assoc and mysql_fetch_object most probably depend on how you develop your application : if using objects everywhere, the second one is probably the most suited.
If using arrays as data-containers, you can just go with the first one.
mysql_fetch_array() fetches a result row as an associative array, a numeric array, or both.
It returns an array of strings that corresponds to the fetched row, or FALSE if there are no more rows. The type of returned array depends on how $result_type is defined.
By using MYSQL_NUM, you only get number indices (as $row[0], $row1, etc) i.e., numeric array.
By using MYSQL_ASSOC, you only get associative indices (as $row["id"], $row["name"], etc) i.e., associative array.
By using MYSQL_BOTH (default), you’ll get an array with both associative and number indices. (as $row[0], $row["name"], etc) i.e., both numeric array and associative array.
mysql_fetch_assoc() fetches a result row as an associative array. (column names as key).
mysql_fetch_object() fetches the result row as an object.
It returns an object with properties that correspond to the fetched row and moves the internal data pointer ahead.
To me there are adantages of using
mysql_fetch_assoc() in that you can use the array functions such as
array_walk and uasort().
Suppose we have following table
Name CountryCode
----- ----------
Bangladesh BD
Pueblo USA
Arvada USA
$query = "SELECT Name, CountryCode FROM City";
$result = mysqli_query($connection, $query)
mysqli_fetch_object (data can be fetch as object)
while ($obj = mysqli_fetch_object($result)) {
printf ("%s (%s)\n", $obj->Name, $obj->CountryCode);
}
mysqli_fetch_assoc (fetch as associative array i.e. key)
while ($row = mysqli_fetch_assoc($result)) {
printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
}
mysqli_fetch_array (numeric & associative both)
while ($row = mysqli_fetch_array($result, MYSQLI_NUM)) {
printf("%s (%s)\n", $row[0], $row[1]);
}
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
printf("%s (%s)\n", $row["Name"], $row["CountryCode"]);
}
Short description:
mysql_fetch_assoc() : This gets you an associative array of data.
mysql_fetch_array() : This returns a combination array of associative elements as well as data with numerical index.
mysql_fetch_object() : Returns an object with properties that correspond to the fetched row.
Source
Related
here's my php code
$result = mysql_query("select * from backup where owner='$email'") or die (mysql_error());
$dataCount = mysql_num_rows($result);
$row = mysql_fetch_array($result);
echo json_encode($row);
and it returns this:
{"0":"1","id":"1","1":"2015","year":"2015","2":"55","necessities":"55","3":"10","savings":"10","4":"10","entertainment":"10"}
this is how jsonviewer.stack.hu shows it
fyi, there's only one row of data inside the table. but it seems json_encode($row) displays the value twice, but firstly using number (0 - 4) as the label, then it uses the column name (id, year, necessities, savings, entertainment) as the label.
how can I make it to display the value only once, using the column name?
Change mysql_fetch_array to mysql_fetch_assoc.
mysql_fetch_array returns a result row in both numeric and associative array.
mysql_fetch_assoc returns a result row as associative array.
http://php.net/manual/en/function.mysql-fetch-array.php
You can give this functional additional arguments including MYSQL_ASSOC, MYSQL_NUM, and MYSQL_BOTH. In your case you want MYSQL_ASSOC.
However, you should be using mysqli and not mysql. the mysql functions are no longer maintained.
http://php.net/manual/en/mysqli-result.fetch-array.php
For new versions of php ( > 5.5), use mysqli_fetch_assoc instead of mysql_fetch_assoc.
The mysql_fetch_assoc function is deprecated and is no longer maintained: Using it will lead to errors.
I have been reading about mysql_fetch_* methods.
This is what I have learnt from PHP.org website.
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_object — Fetch a result row as an object
mysql_fetch_row — Get a result row as an enumerated array
It looks like mysql_fetch_array contains all the values that are present in
mysql_fetch_assoc,mysql_fetch_object,mysql_fetch_row. Because mysql_fetch_assoc contains only Associative array,
mysql_fetch_row contains data in Numeric Array.
mysql_fetch_object also returns associative array.
Kindly tell me whether my understanding is correct or wrong.
mysql_fetch_assoc returns an associative array, mysql_fetch_row returns a numeric array, and with mysql_fetch_array you can chose what would be the output. This function accepts an optional parameter which can take values:
MYSQL_ASSOC - returns associative array
MYSQL_NUM - returns numeric array
MYSQL_BOTH - returns combined numeric and associative array
The last value is default.
mysql_fetch_object is slightly different as it returns an object wich has fields corresponding to columns in result fetched from database.
As a sidenote I would like to add that mysql_* functions are deprecated and you should switch to mysqli or PDO.
I want to know the real difference in PHP PDO between the cast variable and the constant PDO::FETCH_* for the return query
Example:
$row = $stmt->fetch(PDO::FETCH_OBJ);
OR
$row = (object) $stmt->fetch();
There is no significant difference, but obviously the first line does less work: it fetches the data and fills an object, while the second one fetches the data, fills an array and then casts the array to the object.
You could also look at http://us.php.net/manual/en/pdostatement.fetchobject.php
Method stmt->fetch(); return an indexed array (PDO::FETCH_BOTH) by both column name and zero-indexed array and $stmt->fetch(PDO::FETCH_OBJ); method returns anonymous object with property names that correspond to the column names returned in your result set.
How does mysql_fetch_array work?
Is it a stack/queue?
It fetches the current row from the open cursor and hydrates it into an array using one or both of two methods:
MYSQL_ASSOC - column names as array key
MYSQL_NUM - column position as array index
See http://en.wikipedia.org/wiki/Cursor_(databases)
I have the following code and it only works when I specify the column number!
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
echo($row[0]);
}
Not when I use the name though
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
echo($row['name']);
}
Is there something that needs setting on the MYSQL box?
You might want to try print_r( $row ) for debugging so you see which column names are actually set in the resultset. Getting the associative names doesn't need to be configured in some way, but the index names need to exactly represent the column names in the database result.
mysql_fetch_array, according to the documentation, fetches the result row as an numeric array or as an associative array, depending on the second parameter. By default, this is MYSQL_BOTH, so you can access via both means.
It seems like the problem may like with your query, specifically the actual name of the column in your SELECT statement. Without seeing more of your query, I can suggest adding AS name to the first column to ensure it is the actual retrieved column name. print_r will print the array for further debugging.