PHP PDO Select query returns double values
Set the default fetch mode like so
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
Or
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
before running the fetch
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
foreach($result as $row) {
array_push($result_array, $row);
}
Or replace with a one liners using fetchAll()
and use prepared and parameterized statements as well to mitigate against SQL Injection Attack
$query = "SELECT datum, tijd, zone,lucht, temperatuur
FROM waarden
WHERE datum = :datum";
$result->prepare($query);
$result->execute([':datum'=>$waarde]);
$result_array = $result->fetchAll(PDO::FETCH_ASSOC);
The parameter to fetchAll(PDO::FETCH_ASSOC)
controls how results will be returned.
PHP PDO execute() returning duplicate results
Solved by checking with someone better at databases than myself...
I foolishly named the columns as integer values.
So, what appeared as "duplicate values" was just the fact that the column number and the column name were the same!
In conclusion, I am bad at database structure.
PDO prepared statement fetch() returning double results
You should say to PDO, that you want only an associative array or a numbered array:
while ($rows_get_rows = $result_get_rows->fetch(PDO::FETCH_ASSOC))
to get an associative array or
while ($rows_get_rows = $result_get_rows->fetch(PDO::FETCH_NUM))
to get an array indexed by the column number
from PDOStatement::fetch
fetch_style
Controls how the next row will be returned to the caller.
This value must be one of the PDO::FETCH_* constants, defaulting to
value of PDO::ATTR_DEFAULT_FETCH_MODE (which defaults to
PDO::FETCH_BOTH).PDO::FETCH_ASSOC: returns an array indexed by column name as returned
in your result setPDO::FETCH_BOTH (default): returns an array indexed by both column
name and 0-indexed column number as returned in your result set
PDO returning incorrect, but duplicate, data. Key's not in database.
It's not duplicates, it's just the current FETCH_MODE
you're using. To get as associative keys only you need to specify as such; by default it fetches as both.
Use like so:
$query->fetchAll(PDO::FETCH_NUM); // to fetch with numeric indexes
$query->fetchAll(PDO::FETCH_ASSOC); // to fetch with associative indexes
fetchAll docs
fetch docs
sql PDO fetchall duplicate object keys?
You have to specify fetching mode
$q->fetchAll(PDO::FETCH_ASSOC);
PDO: Remove duplicates of the query's results
The first argument of your fetchAll call schould be PDO::FETCH_ASSOC
See http://php.net/manual/en/pdostatement.fetch.php
PHP PDO query returns inaccurate value for FLOAT fields
As documented under Floating-Point Types (Approximate Value) - FLOAT
, DOUBLE
:
MySQL performs rounding when storing values, so if you insert
999.00009
into aFLOAT(7,4)
column, the approximate result is999.0001
.Because floating-point values are approximate and not stored as exact values, attempts to treat them as exact in comparisons may lead to problems. They are also subject to platform or implementation dependencies. For more information, see Section C.5.5.8, “Problems with Floating-Point Values”
For maximum portability, code requiring storage of approximate numeric data values should use
FLOAT
orDOUBLE PRECISION
with no specification of precision or number of digits.
Therefore, upon inserting 1.8
into your database, MySQL rounded the literal to 001.8000
and encoded the closest approximation to that number in binary32 format: i.e. 0x3FE66666
, whose bits signify:
Sign : 0b0
Biased exponent: 0b01111111
= 127 (representation includes bias of +127, therefore exp = 0)
Significand : 0b[1.]11001100110011001100110
^ hidden bit, not stored in binary representation
= [1.]7999999523162841796875
This equates to:
(-1)^0 * 1.7999999523162841796875 * 2^0
= 1.7999999523162841796875
This is the value that is returned by MySQL to the client. It would appear that AdoDB then inspected the column's datatype and rounded the result accordingly, whereas PDO does not.
If you want exact values, you should use a fixed point datatype, such as DECIMAL
.
Related Topics
Strip HTML Tags and Its Contents
PHP Error: Notice: Undefined Index:
PHP Preg-Replace More Than One Underscore
Cakephp Cake_Core_ Cache Was Unable to Write 'Cake_Dev_En-Us'
Laravel 5.1 Date_Format Validation Allow Two Formats
Maximum Length of Generated Hash When Using Password_Hash
How to Handle Newlines in JavaScript? (From PHP)
Warning Problem: Expects Parameter 1 to Be MySQLi_Result
How to Insert Multiple Rows in PHP Pdo MySQL
How to Convert a Huge Integer to Hex in PHP
Redirect to 'Www' Before Ssl Requirement
Php: How to Convert Bigint from Int to String
Php: "Short_Open_Tag = On" Not Working
How to Detect Ambiguous and Invalid Datetime in PHP
How to Add a PHP Simplexmlelement to Another Simplexmlelement