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
Related
I very like the syntax :
$result = $db_connect->query("SELECT * FROM table")->fetch_assoc();
instead of more verbose
$query = $db_connect->query("SELECT * FROM table");
$result = $query->fetch_assoc();
$query->free();
My question is how do I free results from memory in case 1 ?
In other words : Do assigning a new value to the variable does automatically free the results ?
I've searched for that on stackExchange / Google with no success...and I found it's an interesting question.
Method Chaining (your first example) works by executing each following function on the object returned by the previous function. So you can do $db_connect->query("SELECT * FROM table")->fetch_assoc(); because query() returns a mysqli_result object and that object supports the fetch_assoc() method.
fetch_assoc(), in turn, 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.
Since this result is an array (or NULL) and not an object, no further methods can be added to the chain after it.
I'm using PHP to get a value from MySQL. My table has this form:
COL1 = MAIL mail#mail.com
COL2 = NAME myname
and I'm getting from the database the row of the user.
$query = mysqli_query($conn, "select * from user where mail = 'telmo#mail.com'");
$row = mysqli_fetch_array($query);
print_r($row);
But my output is very strange, it gives my like a bidimensional array like this:
Array ( [0] => mail#mail.com [MAIL] => mail#mail.com [1] => myname [NAME] => myname )
I'm learning PHP and I'm wondering if is this supposed to be like this. Because I would want something more like:
MAIL => mail#mail.com
NAME => myname
Maybe I'm not getting this syntax very well, but this is why I'm looking for help.
Thanks in advance
Thank you in Advance
From the manual
mysqli_fetch_array() is an extended version of the mysqli_fetch_row() function. In addition to storing the data in the numeric indices of the result array, the mysqli_fetch_array() function can also store the data in associative indices, using the field names of the result set as keys.
What you you're really looking for is mysqli_fetch_assoc()
Returns an associative array that corresponds to the fetched row or NULL if there are no more rows.
Either specify MYSQLI_ASSOC after your query, or use mysqli_fetch_assoc. from the manual:
resulttype This optional parameter is a constant indicating what type
of array should be produced from the current row data. The possible
values for this parameter are the constants MYSQLI_ASSOC, MYSQLI_NUM,
or MYSQLI_BOTH.
By using the MYSQLI_ASSOC constant this function will behave
identically to the mysqli_fetch_assoc(), while MYSQLI_NUM will behave
identically to the mysqli_fetch_row() function. The final option
MYSQLI_BOTH will create a single array with the attributes of both.
Manual: http://us2.php.net/mysqli_fetch_array
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.
example:
$query = $mydb->query('PRAGMA table_info("mytable")');
//variable query has now type of PDOStatement Object
print_r($query->fetchAll(PDO::FETCH_COLUMN,1)); // this result is ok
print_r($query->fetchAll(PDO::FETCH_COLUMN,2)); // but this result gives empty array
so is there any way to reuse statement object ?
A PDOStatement Object returns false when there are no more rows available. The call to fetchAll covers all rows which would then always return false on any following attempt to fetch. This limits the reuse of the statement object. You can use the PDOStatement::bindColumn to achieve what it looks as if you are attempting in your example.
$query = $mydb->query('PRAGMA table_info("mytable")');
// Variables passed to be bound are passed by reference.
// Columns are based on a *1* index or can be referenced by name.
$query->bindColumn(1, $bound_column1);
$query->bindColumn(2, $bound_column2);
$query->fetchAll();
print_r($bound_column1);
print_r($bound_column2);
Sure, you can re-use the statement object.
But think about this for a moment: If you already have fetched all PHP Manual, why do you expect that the second fetch all statement can still return something?
print_r($query->fetchAll(PDO::FETCH_COLUMN,1)); // this result is ok
print_r($query->fetchAll(PDO::FETCH_COLUMN,2)); // but this result gives empty array
PDO has a cursor that advances. Once at the end, there is nothing you can do but close the cursor PHP Manual and execute again.
You can re-use the statement object for that.
But I don't think you want/need to actually do that for your code. Instead, fetch all columns and then access them in the returned data by their index:
print_r($query->fetchAll(PDO::FETCH_NUM)); // this result is ok
You will see in the output that you now have 0-indexed column numbers per each row, so you can access the columns within the return data by their column-index - row per row.
$array = $query->fetchAll( args );
Then use the $array how you want.
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.