How can I retrieve query results as array in PDO? - php

Im working on a small PHP project and im changing from MySQLi to PDO , im also using prepared statements now.
I have managed to connect to the database and execute a query but I want to know how I can retrieve the results as an array?
I use the fetch_assoc() function in MySQLi to do this but now im using PDO and Prepared statements , i dont know how to accomplish this.
My code so far...
$res = $connection -> prepare("SELECT * FROM ad WHERE id = :id");
$res -> bindValue(':id',$id);
$res -> execute();
I have heard of using this code but it doesnt work?
$z= $res -> fetchAll(PDO::FETCH_ASSOC);
$productname = $z['productname'];

$z is now a multi-dimensional array, and as such, if you want to access the first row, you could just explicitly access index zero:
echo $z[0]['productname'];
It should look something like this:
Array
(
[0] => Array // index zero
(
[id] => 1
[productname] => productname 1 // sub index
)
// $z[0]['productname']
[1] => Array
(
[id] => 2
[productname] => productname 2
)
// $z[1]['productname']
[2] => Array
(
[id] => 3
[productname] => productname 3
)
// $z[2]['productname']
)
If you want to access all the row sets, just use the good ol' foreach:
foreach($z as $row) {
echo $row['productname'];
}

Related

SQL result array, from PDO query, has duplicated values [duplicate]

This question already has answers here:
sql PDO fetchall duplicate object keys?
(2 answers)
Closed 2 years ago.
I have a mysql table that has the following fields
STUDENTID | FIRSTNAME | SURNAME | DOB | SCHOOLID
A form is used for a user to search for a student and output all those with matching first and surnames
The following SQL statement returns the PDO fine
$sqlstmnt2 = 'SELECT * FROM students WHERE firstName = :fname AND surname = :sname AND schoolID = :schoolID';
// prepare PDOstatement as $query ...
// ...
$query->execute();
$_SESSION['foundPupils'] = $query->fetchAll();
However, when I pass this through to another PHP page in a session variable, I'm confused as to how to access each field individually. I have the following code
foreach($_SESSION['foundPupils'][0] as $found){
echo($found);
}
This outputs the found data but the issue is that it outputs it twice, and it's just a long string of data which can't be formatted nicely. My questions are:
Why does each result output twice?
How do I access the individual fields within this array (kind of like foundPupils[0]['firstName'] for example?
According to the documentation, both the method PDOStatement::fetch and PDOStatement::fetchAll accepts a $fetch_style parameter. If not set, the default value is PDO::FETCH_BOTH. That means the fetched array will both reference the fields by position (e.g. 0, 1, 2) and by column name (e.g. "firstName", "surname").
So by default, your fetched results would be something like this:
Array
(
[0] => Array
(
[name] => apple
[0] => apple
[colour] => red
[1] => red
)
[1] => Array
(
[name] => pear
[0] => pear
[colour] => green
[1] => green
)
[2] => Array
(
[name] => watermelon
[0] => watermelon
[colour] => pink
[1] => pink
)
)
To fix this, you need to call your fetch function (I suppose it is PDOStatement::fetchAll) with PDO::FETCH_COLUMN as the fetch style:
$query->execute();
$_SESSION['foundPupils'] = $query->fetchAll(PDO::FETCH_COLUMN);
Query results are returned and stored in a result object. Therefore, you have to iterate through the result either using xxxx_fetch_array or xxxx_fetch_row. for example
$sql='SELECT * FROM table_name';
$result = mysqli_query($conn,$sql);
while($row=mysqli_fetch_array($result)){
echo $row['COLUMN_NAME'].'<br />';
}

PHP PDO SQL only returning one row of data instead of all rows

I am using PHP PDO extension to create a list of all items from a DB category table.
The expected results are not correct, only one row of data returned instead of all
rows which is expected.
By running the same SQL statement within the phpMyAdmin console I get the expected
results for all rows of data.
I need to know what I am overlooking with pdo.
Current code:
$catId = 0;
$sql = "SELECT *
FROM category
WHERE cat_parent_id = :catId
ORDER BY cat_name";
$_stmt = $this->_dbConn->prepare($sql);
$_stmt->bindParam(":catId", $catId, PDO::PARAM_INT);
$_stmt->execute();
$rows = $_stmt->fetch(PDO::FETCH_ASSOC);
// display PDO result - only getting one row instead of all rows.
print_r($rows);
Array ( [cat_id] => 3
[cat_parent_id] => 0
[cat_name] => Shop
[cat_description] => Seminars
[cat_image] => c72e.gif
)
// NOTE: By running the same SQL within the phpMyAdmin
// console I get the expected results with all rows.
Array ( [cat_id] => 3
[cat_parent_id] => 0
[cat_name] => Shop
[cat_description] => Seminars
[cat_image] => c72e.gif
),
( [cat_id] => 1
[cat_parent_id] => 0
[cat_name] => Site Map
[cat_description] => List content links
[cat_image] => c83b.gif
)
PDOStatement::fetch() will only return 1 row at a time.
You could use PDOStatement::fetchAll():
$rows = $_stmt->fetchAll(PDO::FETCH_ASSOC);
or create a loop where you keep calling PDOStatement::fetch() until it returns false:
$rows = array();
while( $row = $_stmt->fetch(PDO::FETCH_ASSOC) ) {
$rows[] = $row;
}
But the latter example is a bit redundant, if you want to get all rows at once anyway, unless there are some memory concerns you want to address.
You have to use fetchAll like below:
$rows = $_stmt->fetchAll();
print_r($rows);

PHP PDO execute() returning duplicate results

For this code:
$title = '0';
$subTitle = '1';
$tableName = 'someString';
$stmt = $dbh->prepare("SELECT `".$title."`, `".$subTitle."` FROM `".$tableName."_data` WHERE `id` = :key");
$stmt->bindValue(':key', '127');
$stmt->execute();
$result = $stmt->fetchAll();
print_r($result);
I get $result as this:
Array
(
[0] => Array
(
[0] => 91651
[1] => 91651 - DESCRIPTION
[2] => 91651 - DESCRIPTION
)
)
When the expected result should be this:
Array
(
[0] => Array
(
[0] => 91651
[1] => 91651 - DESCRIPTION
)
)
When I run the same query in mySQL, it returns the expected result. When it is executed via PHP PDO, it adds a duplicate.
Thanks!
Use fetchAll(PDO::FETCH_ASSOC) instead of fetchAll().
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.
Try to check the fetch_style and fetch_argument and set it to a value you need.
see sample #3:
http://www.php.net/manual/en/pdostatement.fetchall.php
sometimes dep. on your php version/pdo version there is a bug when using bindvalue.
try to avoid that and use sample #3 instead.
You could change to using fetch() instead of fetchAll(), that will return one row. But I'd just ensure in the database that id is unique.

PHP class array question

For some reason my array I am returning is not what I expect. Could someone explain to me why I am getting the current results, and what I can do to fix it? Here is the code in question:
public static function getProduct($_row, $_value)
{
$stmt = _DB::init()->prepare("SELECT pid, name, quantity, price, cost
FROM products
WHERE $_row = ?"
);
if($stmt->execute(array($_value)))
{
while ($row = $stmt->fetch())
return $row;
}
}
$product = Class::getProduct('pid',1);
print_r($product);
When I print the following array I am getting two results per row like so:
Array ( [pid] => 1 [0] => 1 [name] => Boondoggle [1] => Boondoggle [quantity] => 12 [2] => 12 [price] => 9.9900 [3] => 9.9900 [cost] => 12.9900 [4] => 12.9900 ) Boondoggle
I was only wanting to show the associative results. What is wrong with my function?
From the looks of it you are using PDO to communicate with your DBMS. The PDOStatement::fetch() method's first argument is a parameter to tell it what to return. By default it returns each column in both name format and numbered index format to allow iterating through columns easier. To just get the column names as indexes, you can pass it PDO::FETCH_ASSOC to your call. So the fetch statement would look like this:
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)
See here for more details:
http://www.php.net/manual/en/pdostatement.fetch.php
Pass PDO::FETCH_ASSOC to your fetch call:
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
edit: I'm just assuming you're using PDO, of course

PDO acts different on two -very- similar queries

The following block of code works fine (no errors)
$query = "select * from users where username = ?";
$statement = $sql->prepare($query);
echo gettype($statement); // -- This returns 'object'
$statement->bindParam(1, $username);
The following gives:
Fatal error: Call to a member function bindParam() on a non-object in /file.php on line 39
$email = 'fake#email.com';
$query = "select * from users where email = ?";
$statement = $sql->prepare($query);
echo gettype($statement); // -- this returns 'boolean'
$statement->bindParam(1, $email); // -- this is line 39.
Now this is strange.
At my local machine, and my remote host, this was never a problem.
This errors only shows up on this new hosting company I am trying out for the month. Could it be a config param when they compiled php?
--------edit--------
While still trying to figure out what's wrong,I found this out.
$query = "select userID, username from users";
$statement = $sql->prepare($query);
$statement->execute();
$r = $statement->fetchAll(PDO::FETCH_ASSOC);
// display # of rows
echo "Rows returned: " . $statement->rowCount();
// display results array
echo '<pre>'; print_r($r); echo '</pre>';
On a server, I get
Rows returned: 4
Array
(
[0] => Array
(
[userID] => 1
[username] => lyrae
)
[1] => Array
(
[userID] => 2
[username] => jproffer
)
[2] => Array
(
[userID] => 3
[username] => king
)
[3] => Array
(
[userID] => 4
[username] => gergy
)
)
Which is correct. Says 4 rows returned and displays the result array. On another server however, I get
Rows returned: 0
Array
(
[0] => Array
(
[userID] => 1
[username] => lyrae
)
[1] => Array
(
[userID] => 2
[username] => jproffer
)
[2] => Array
(
[userID] => 3
[username] => king
)
[3] => Array
(
[userID] => 4
[username] => gergy
)
)
Thus, it seems also that PDOStatement::rowCount() does not work on a sever but works on another.
Read this: $statement->closeCursor()
PDOStatement::closeCursor() frees up the connection to the server so that other SQL statements may be issued
Are you using the same database on the server where you say you don't have this issue?
Found a solution to the problem.
This is the entire block of code..
// check if username exists
$query = "select * from users where username = ?";
$statement = $sql->prepare($query);
$statement->bindParam(1, $username);
$statement->execute();
// check if email exists
$sql2 = new PDO('mysql:host=localhost; dbname=db', 'username', 'pw');
$query = "select * from users";
$statement = $sql2->prepare($query);
echo gettype($statement);
#$statement->bindParam(1, $email);
So for some reason, I have to create a new instance of PDO. what's strange is that on 2 other servers, I don't have to do this.
And upon further looking, i found that PDO::Prepare raises an PDOExeption.
Here it is:
SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.
Array
(
[0] => HY000
[1] => 2014
[2] => Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.
)
Maybe it'll help someone in the future :)
Is $email undefined? You could try a var_dump($email) too see what it says. Good luck.
Have you tried putting the $email= line below the bindParam (but before you do the execute)? bindParam passes the parameter by reference so you can execute as query, change the value of the variable and execute again.
I think though that it's probably a PHP set up error. I've heard people saying PDO had a lot of bugs before PHP 5.3, so maybe see if you can get PHP up to the latest version?
Have you also tried swapping the two queries around? Maybe something breaks after you run one query.
I'd better recommend using this:
$email = 'fake#email.com';
$query = "select * from users where email = ?";
$statement = $sql->prepare($query);
$statement->execute(array($email));
No BindParam use is needed here.
I had same problem with
echo "Rows returned: " . $statement->rowCount();
I got -1 rows, lol. My database is INFORMIX and searching web i found
that rowCount(); returns only affected rows in a DELETE, INSERT, or UPDATE statement executed by the corresponding PDOStatement object.
With a SELECT statement you need to use function
$statement->fetchColumn();
read it here: http://www.phpbuilder.com/manual/en/function.pdostatement-rowcount.php

Categories