Reputation: 59
Hi I am trying to query a database using pdo in php to return all of the information in my database, I am connecting properly to the database however when I try to return all of the database information and print it I get this result returned from my function >
PDOStatement Object ( [queryString] => SELECT * FROM users )
I would like for the function to return the entire database information. Here is my code, your help would be greatly appreciated, thank you.
public function resetpassword()
{
try
{
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql = 'SELECT * FROM users';
$result = $conn->query($sql);
return $result;
$conn = null;
}
catch(PDOException $e)
{
return 'Database Error';
}
}
here is the code I use to print the results from that function:
print_r ($usr->resetpassword());
Upvotes: 0
Views: 486
Reputation: 1527
$result is result set as PDOStatement object. If you use fetchAll()
it will fetch data as an array of data. Although you can loop through PDOStatement object result set and extract data. Like this:
foreach ($conn->query($sql) as $row) {
echo $row['yourColumnName'];
}
Try this:
<?php
class Database{
public function resetpassword()
{
try
{
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql = 'SELECT * FROM users';
$s = $conn->query($sql);
return $s->fetchAll(PDO::FETCH_ASSOC);
$conn = null;
}
catch(PDOException $e)
{
return 'Database Error';
}
}
}
$usr=new Database();
var_dump($usr->resetpassword());
Upvotes: 1