Question about mysql_fetch_array? - php

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)

Related

MySQLi associative array of results in PHP 5.2.6

Is there a way to fetch an associative array of results in MySQLi using PHP 5.2.6?
I know is PHP 5.3+ you can use get_result(). I know there is bind_result() in PHP 5.2.6 although there can be any number of columns in the query (not a set number.)
Been pulling my hair out over this one, ANY help will be appreciated!
Procedural :
mysqli_fetch_assoc ( mysqli_result $result )
Object Oriented :
mysqli_result::fetch_assoc ( void )
More info : http://php.net/manual/en/mysqli-result.fetch-assoc.php
Your friend is mysqli_fetch_assoc.It Fetches a result row as an associative array.
From the doumentation..
Returns an associative array of strings representing the fetched row in the result set, where each key in the array represents the name of one of the result set's columns or NULL if there are no more rows in resultset

Is mysql_fetch_array a the combination of mysql_fetch_assoc and mysql_fetch_row?

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.

Difference between PDO::FETCH_* and the casting

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.

PHP fetch MySQL array with an index

When fetching an array from MySQL the rows are typically returned with a key from 0 to the size of your recordset:
row[0][key][value]
Is it possible to have one of the fields from the select statement returned as the key in the array?
For example. Assuming my data set has StudentID, Name, City, etc.
How can I select into an array where I could refer to the StudentID as the index like this:
rows[StudentID][Name]
rows[StudentID][City]
etc.
Thanks!
PDOStatement::fetchAll
To return an associative array grouped by the values of a specified column, bitwise-OR PDO::FETCH_COLUMN with PDO::FETCH_GROUP.
// Other PDO stuff to get a statement - abstract below
$result = PDOStatement::fetchAll( PDO::FETCH_COLUMN | PDO::FETCH_GROUP, 0 );
See example 3 on this page
Depending on which library you are using:
mysql_fetch_assoc()
mysqli_fetch_assoc()
PDO fetches both by default.

mysql_fetch_array, mysql_fetch_assoc, mysql_fetch_object

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

Categories