Call to a member function fetch() on integer in - php

I have a problem with a simple pdo query here the query:
$NU=$connection->exec("SELECT COUNT(ID) AS Total FROM USERS");
$Result=$NU->fetch(PDO::FETCH_ASSOC)['Total'];
echo "$Result";
Since I have no params to bind in the query is correct to use exec without prepare, and how can I fix this problem? (Call to a member function fetch() on integer in )

The exec() method only returns the number of rows effected. You probably want to use query() instead.
$NU=$connection->query("SELECT COUNT(ID) AS Total FROM USERS");
$Result=$NU->fetch(PDO::FETCH_ASSOC)['Total'];
echo "$Result";
The query() statement will execute a single query and return a PDOStatement object you can fetch from or false on failure.

You need to use query http://php.net/manual/en/pdo.query.php , then you'll have a object with results you can work with.

Try this.
$NU = $connection->query("SELECT COUNT(ID) AS Total FROM USERS");
$result = $NU->fetch();
echo $result['Total'];

What you are looking for is not exec but prepare. From the PHP doc: http://php.net/manual/en/pdostatement.fetch.php
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();
$result = $sth->fetch(PDO::FETCH_ASSOC);
print_r($result);

Related

PDO::Fetch_classtype returns std class instead of first argument of sql query

$sql = "select classname,id,name,value,description from tablename";
$query = $this->conn->prepare($sql);
$query->execute();
$result = $query->fetch(PDO::FETCH_CLASS | PDO::FETCH_CLASSTYPE);
var_dump($result);
Result shows stdClassObject
Expected is the classname object (of the particular row)
Basically, am trying to loop through the result, figure out which class the row belongs to and access their getter functions. Same is achieved if I use the following, but not the solution that works for me.
$sql = "select classname,id,name,value,description from tablename";
$query = $this->conn->prepare($sql);
$query->execute();
$result = $query->fetch(PDO::FETCH_CLASS, 'MyClass');
var_dump($result);
Now, I can loop through the result and access $result->getId() or $result->getName() etc. How I can achieve this through PDO::FETCH_CLASSTYPE as the classname is dynamically decided by the row in the result set.

Placing a variable into a PDO Statement

is it possible to create a pdo query with a variable? Example:
$q = "SELECT COUNT (*) c FROM blogpages WHERE keywords LIKE '%test%' ";
then
$query = $db->query("$q");
$result = $query->fetch(PDO::FETCH_ASSOC);
when i do this i get an error
"Call to a member function fetch() on a non-object in C....."
i want to know is there a way to place the query in there as a variable because the query changes depending on how many OR statements are in the query
query()
PDO::query — Executes an SQL statement, returning a result set as a
PDOStatement object
And problem in your query with space between count and (*)
SELECT COUNT (*)..
^^
SO no need to fetch data just use
$q = "SELECT COUNT(*) c FROM blogpages WHERE keywords LIKE '%test%' ";
foreach ($db->query($q) as $row) {
print $row['c'] . "\t";
}

mysqli counting results and fetching them

Hello I have a prepared statement and I need to count the number of results I get. In order to do this I use store_result and num_rows
$query = 'SELECT userId, promo, email FROM users WHERE active = ?';
$rsActivation = $db->prepare($query);
$rsActivation->bind_param('s', $actv);
$rsActivation->execute();
$rsActivation->store_result();
$totalRows = $rsActivation->num_rows;
This code manages to get me the number of rows. The problem is that if I do this I cannot use fetch() on $rsActivation. If I use fetch and not use store_result I cannot get the number of rows.
How can I accomplish both things?
Thanks
SOLVED:
Turns out my problem was I was trying to fetch the results as an associative array. Instead I used bind_result to assign values to variables. Then I was able to use store_result and num_rows to get the count and after that I used fetch() together with the variables I assigned in bind_result.
$query = 'SELECT userId, promo, email FROM users WHERE active = ?';
$rsActivation = $db->prepare($query);
$rsActivation->bind_param('s', $actv);
$rsActivation->execute();
$rsActivation->bind_result($userId, $promo, $email);
$rsActivation->store_result();
$totalRows = $rsActivation->num_rows;
while($rsActivation->fetch()){
echo "<p>". $userId ."</p>";
...
}
You can try using
...
$rsActivation->execute();
$results = $rsActivation->get_results();
$totalRows = $results->num_rows;
and you should be able to fetch using something like
$results->fetch_assoc(), $results->fetch_row(), etc.
Here's the doc for it: http://php.net/manual/en/class.mysqli-result.php

Php pdo result array

I have a select to a database what should get a number and display it, but display just array
$sth = $conn->prepare("SELECT score FROM people WHERE email='gasd3z#yaho.com'");
$sth->execute();
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
echo $result;
What can I do to display that number?
You don't prepare anything so you don't need to use prepared statements just use query like this:
$result = $conn->query("SELECT score FROM people WHERE email='gasd3z#yaho.com'");
foreach($result as $row)
echo $row['score'];
The previous answer is wrong, but not much.
When you're doing fetchAll(), you're getting all of the rows matching query from the database.
The answer is
$sth = $conn->prepare("SELECT score FROM people WHERE email=:email");
$sth->execute([':email' => 'gasd3z#yaho.com']);
$result = $sth->fetch(PDO::FETCH_ASSOC);
$sth->closeCursor();
echo $result['score'];
And don't forget that prepare() is for preparing statements, so you better want to have user data in query just like in my example. If you don't want to process any values in query, use query() or exec() instead.
You can print out arrays with print_r($result) or var_dump($result). Echoing only prints out the type.
Also, if you only need to print out a specific line, do it like echo $result['score'];

PDO Fetch Array like MySQLi

I know how to fetch a PDO array, but how do I collect data from it like you do with MySQLi's fetch_array?
For example,
MySQLi
$query = $mysqli->query("SELECT * FROM `foo` WHERE `ID`='1'");
$array = $query->fetch_array();
Getting a result
echo $array['bar'];
How would you do this with PDO? I understand you can do this:
PDO
$query = $pdo->prepare("SELECT * FROM `foo` WHERE `ID`='1'");
$query->execute();
$result = $query->fetchAll();
Getting the result
echo $result['bar'];
Does not return the same as MySQLi did
Am I doing something wrong, and is there a way of doing this?
fetchAll() is not the same as fetch_array().
You want fetch() to get one row, not fetchAll() which gets ALL rows.
$result = $query->fetch(PDO::FETCH_ASSOC);

Categories