$GetUid = $dbConnect->prepare("SELECT UID FROM users WHERE username = :username");
$GetUid->execute($RegisterData3);
$UserID = $GetUid->fetch();
why does it return array not a string ?
var_dump('$UserID') says
array
'UID' => string '45' (length=2)
0 => string '45' (length=2)
it should be
array
'UID' => string '45' (length=2)
update* what about the 0 ? where does it came from ? thanks for the replies.
Introduction to the PHP fetch() method Therefore, the subsequent call to the fetch() method will return the next row from the result set. To fetch all rows from a result set one by one, you typically use the fetch() method in a while loop. The fetch() method accepts three optional parameters.
By default, PDO returns each row as an array indexed by the column name and 0-indexed column position in the row. To request a different return style, specify one of the following constants as the first parameter when you call the PDOStatement::fetch method: PDO::FETCH_ASSOC.
Return ValuesPDOStatement::fetchAll() returns an array containing all of the remaining rows in the result set.
Return Values ¶ PDOStatement::fetchAll() returns an array containing all of the remaining rows in the result set. The array represents each row as either an array of column values or an object with properties corresponding to each column name. An empty array is returned if there are zero results to fetch.
You didn't specify a fetch_style
parameter. It returns FETCH_BOTH
by default, which is an array. Here are the options and how to specify it:
http://php.net/manual/en/pdostatement.fetch.php
EDIT: Also, it will always return an array, even if there's only one column, because a row can contain multiple attributes. You can use FETCH_ASSOC
and then specify your column name to get the data, or, if you just use fetch()
like you did, the array is indexed by both the column name and the 0-indexed column number.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With