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
Related
Array([0] => stdClass Object([max(Transaction_id)] => 10251))
My php sql query returns the answer in this format.
How can i display the only value from this format.
Thanks in advance
In your query add the as then the name of the column that you want to retreive.
SELECT MAX(COLUMNNAME) AS example example FROM TABLENAME
And then in your php file
echo $result[0]['example'];
if for some reasons you are unable to convert the sql query you can access to this variable in the following way
echo $obj->{"max(Transaction_id)"};
The biggest problem is that I don't know what words to use. I think I'm looking for an argument or a flag...?
I'm getting an array back from a database fetch and the array stores each value twice, once with a word key and once with a number key..
[0] => Value1
[FirstEntry] => Value1
[1] => Value 2
[SecondEntry] => Value2
I'd like the array to only contain (fetch), or only print, either/or entry of Value1, but not both entries of Value1. How can I do this in PHP, preferably during the PDO fetch or during the print_r/echo loop? Thank you.
Looks like you are fetching data using a "both" fetch style, ie PDO::FETCH_BOTH, MYSQLI_BOTH, etc. These tend to be the default fetch styles.
If you just want a single style of index, use either a "num" or "assoc" style.
For example
$stmt = $pdo->prepare(...);
$stmt->execute();
$stmt->fetch(PDO::FETCH_ASSOC);
$stmt->fetch(PDO::FETCH_ASSOC);
Use This comments (PDO::FETCH_ASSOC) get unique Key Array From return From Data Base ..
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
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.
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.