Call to undefined method PDOStatement::numColumns() - php

I can't run the method numColumns() on my $result.
My code:
try {
$db = new PDO('sqlite:temps.sqlite',null,null,array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
} catch (PDOException $e) {
print 'Exception : ' . $e->getMessage();
}
$result = $db->query("SELECT * FROM data LIMIT 30");
$fields = $result->numColumns();
The error is the following one:
Uncaught Error: Call to undefined method PDOStatement::numColumns() in /*/export.php:7 Stack trace: #0 {main} thrown in /*/export.php on line 7
(line 7 is the last one)
Why does it not work?

You are trying to execute functions from different APIs. You call for PDO, but trying to execute numColumns from SQLite3Result.
As the docs state, PDO::query() returns a PDOStatement object and your error also points you to that:
Call to undefined method PDOStatement::numColumns()
You need to execute columnCount instead:
$result = $db->query("SELECT * FROM data LIMIT 30");
$fields = $result->columnCount();
Or if you wish to work with SQLite3, initialize an SQLite3 connection:
$db = new SQLite3('mysqlitedb.db');
$result = $db->query("SELECT * FROM data LIMIT 30");
$fields = $result->numColumns();

Related

PHP8 PDO SQLite3 execute => fetch* => Fatal error: Uncaught Error: Call to a member function fetch() on bool [duplicate]

This question already has answers here:
Chaining PHP PDO query
(3 answers)
Closed 5 months ago.
I like to use prepared Statements on SQL-calls to a SQLITE3 Database, so i have to use "exectue()" on statements. Fetching results stucks on error "Fatal error: Uncaught Error: Call to a member function fetch() on bool".
try {
$SQL = "SELECT * FROM myTable";
// this works fine:
$result = $db->query($SQL)->fetch(SQLITE3_ASSOC);
print_r($result); // actually delivers 1 recordset
// this not:
$stmt = $db->prepare($SQL);
$result = $stmt->execute()->fetch(SQLITE3_ASSOC); // <= Error
print_r($result);
} catch (PDOException $e) {
$ret['error'] = $e->getMessage();
} catch (Exception $e) {
$ret['error'] = $e->getMessage();
}
die(json_encode($ret));
=> "Fatal error: Uncaught Error: Call to a member function fetch() on bool in..."
Same on fetchArray(). What's wrong with this code?
Please have a look at: https://www.php.net/manual/en/pdostatement.execute.php
The return values of execute are only "true" or "false"
$stmt->execute()
$result = $stmt->fetch(SQLITE3_ASSOC);

Call to a member function fetch() on a non-object

I'm getting the error:
Call to a member function fetch() on a non-object
The line this refers to is:
$getProjectIdResult = $stmt->fetch();
Now, I think from this error that there must be something wrong with my database query, since the documentation says PDO query returns false on failure. I'm having trouble identifying what is causing the issue.
I've tried wrapping the fetch in a try/catch, with
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
However the catch isn't triggered and I just get the original fatal error so I haven't been able to get a more specific error.
classes.php
class Query extends Connection {
public function getProjectID($surveyID) {
$query_getProjectID = "SELECT projectID FROM test WHERE surveyID = :surveyID";
$query_getProjectID_params = array(
':surveyID' => $surveyID
);
try {
$stmt = $this->db->prepare($query_getProjectID);
$stmt = $stmt->execute($query_getProjectID_params);
}
catch (PDOException $ex) {
die("Failed to get project ID: " . $ex->getMessage());
}
$getProjectIdResult = $stmt->fetch();
$getProjectID = $getProjectIdResult['projectID'];
return $getProjectID;
}
}
test.php
include_once("includes/classes.php");
include_once("includes/functions.php");
// Bind $_GET data
// localhost/panel/test.php?surveyID=3&status=1&respondentID=666
// Expected result: 111
$surveyID = sanitise($_GET['surveyID']);
$status = sanitise($_GET['status']);
$respondentID = sanitise($_GET['respondentID']);
$con = new Connection();
$query = new Query();
$query->getProjectID($surveyID);
$con->closeConnection();
I've ruled out the sanitise function causing an issue by testing with and without it.
I apologise as I know this is probably just another amateur making another amateur mistake judging by how many posts there are by the same title.
When you call
$stmt = $stmt->execute($query_getProjectID_params);
You assign the return-value of execute() to $stmt, overwriting the variable, making it a boolean instead of an object. When you continue, $stmt no longer holds the PDOStatement object, but is now a boolean.
The solution is simply to remove the overwrite of your object, like this (remove $stmt = in front).
$stmt->execute($query_getProjectID_params);
http://php.net/pdostatement.execute

Cannot use object of type PDOException as array

i'm currently finishing my website and all the stuff works just fine
but i keep getting a error. It don't break anything but it's annoying to see.
I have been trying alot of stuff but i need some new eyes on this problem :/
Error
PHP Fatal error: Cannot use object of type PDOException as array in
/home/thecodin/public_html/memberlist.php on line 114
Code
Line 113: foreach($MemberList as $MemberListEach) {
Line 114: $MemberGroup = $MemberListEach['Group'];
Line 115: $MemberListGroup = $Class_Users->group_info($MemberGroup);
Database Structure
http://gyazo.com/785f780e6b62df6136087070d7c69c65
Member List Class
public function Member_List($offset, $max)
{
try
{
// Run Query - Member List
$member_list = $this->db->prepare("SELECT * FROM `Users` ORDER BY `Group` DESC LIMIT ".$offset.",".$max."");
$member_list->execute();
$member_list_fetch = $member_list->fetchAll();
return $member_list_fetch;
} catch(PDOException $e) {
return array("ERROR", $e);
}
}
Group Info Class
public function group_info($id)
{
try
{
// Run Query - Group Info
$group_info = $this->db->prepare("SELECT * FROM `Groups` WHERE `GID`=:id");
$group_info->bindParam(':id', $id);
$group_info->execute();
$group_info_rows = $group_info->fetch();
return $group_info_rows;
} catch(PDOException $e) {
return array("ERROR", $e);
}
}
Problem Solved.
I just had to change the return to a string insted of an array in the PDOException
like Loz Cherone said.
I hope i'm done this right.
"/home/thecodin/public_html/memberlist.php on line 114"
line 114 - $MemberListEach['Group']; /* this is array use... */
try dumping object with var_dump

How to select multiple query and retrieve them

When I retrieve two tables there's an error. What is the problem with my code? I have no idea how to fix this
<?php
include ('includes/config.php');
$mysqli = new mysqli(DB_SERVER, DB_UNAME,DB_PASSWD,DB_NAME);
if(!$mysqli){
throw new Exception($mysqli->connect_error, $mysqli->connect_errno);
}
$jqry = $mysqli->prepare("SELECT time FROM table_time ORDER BY time");
if (!$jqry){
throw new Exception($mysqli->error);
}
$jqry->execute();
$jqry->bind_result($time);
$jqry->store_result();
$times = array();
while ($jqry->fetch()){
$times[] = $time;
}
$jqry->close();
$gqry = $mysqli->prepare("SELECT table_group.group FROM table_group.table_group ORDER BY group");
if(!$gqry){
throw new Exception($gqry->error);
}
$gqry->execute();
$gqry->bind_result($group);
$gqry->store_result();
$groups = array();
while ($gqry->fetch()){
$groups[] = $group;
}
?>
This is the error I got:
Notice: Trying to get property of non-object in C:\xampp\htdocs\~Jeremiah\system5\joborder.php on line 31
Fatal error: Uncaught exception 'Exception' in C:\xampp\htdocs\~Jeremiah\system5\joborder.php:31 Stack trace: #0 {main} thrown in
your SQL just before the line 31 is invalid. Try executing it against db directly.
from the manual:
mysqli_prepare() returns a statement object or FALSE if an error occurred.
...and thats why we don't suppress notices in dev enviroment! :)

php - Call to a member function prepare() on a non-object in

I get this error:
Fatal error: Call to a member function prepare() on a non-object in /home/folder/public_html/includes/name.php on line 1768
This is the function:
function _FC_GET($typ, $data, $username){
global $dbh;
$stmt = $dbh->prepare("SELECT * FROM stats_clicks WHERE typ=:typ AND user=:username AND data=:data LIMIT 1;");
$stmt->bindParam(':typ', $typ);
$stmt->bindParam(':username', $username);
$stmt->bindParam(':data', $data);
try {
$stmt->execute();
}
catch(PDOException $e) {
die( _OP_ERROR($e->getMessage(), __FILE__ ,__LINE__) );
}
$QUERY_DAT = $stmt->fetchAll();
return empty($QUERY_DAT['value']) ? 0 : $QUERY_DAT['value'];
}
And this is line 1768:
$stmt = $dbh->prepare("SELECT * FROM stats_clicks WHERE typ=:typ AND user=:username AND data=:data LIMIT 1;");
I can't seem to find what is causing this. I use the $dbh-prepare(); statement in other functions in the same file.
$dbh is not defined at that line. Check where you create the object $dbh if it is before the line 1768 or it is not in any condition which is not fulfilled.
Try this :
Look at here
Probably the cause of your connection.

Categories