Error inserting Object of class Pdostatement database could not be converted to string

Asked

Viewed 3,752 times

-2

I’m trying to insert some data into a table using PHP and some errors are happening.

This is the way I’m using to insert:

$cnpj = "000001";
$sth = $lokos->prepare("INSERT INTO CLENTES (CNPJ) VALUES (?)"); 
$sth->bindParam(1, $cnpj, PDO::PARAM_STR);
$sth->execute(); 
$result = $sth->fetch(PDO::FETCH_ASSOC);
echo $sth;

I’m trying to insert data into the table CLENTES in the CNPJ field and this error is happening:

( ! ) Catchable fatal error: Object of class PDOStatement could not be converted to string in C:\wamp\www\site\index.php on line 30
Call Stack
#   Time    Memory  Function    Location
1   0.0000  134952  {main}( )   ..\index.php:0

What can I do to fix this?

  • According to the error is saying that the function failed to convert the value to string, and if you worked with values int? The table name really is CLENTESor CLIENTES

  • 3

    A lot of people have given different answers. State whether your question has been resolved or not, so that they can help you or learn that you have already been helped. And click on the "accept as answer" button in the answer that helped you.

4 answers

5

The problem is you’re trying to give echo $sth...
Only $sth is not string type. Hence the error.

Just erase the echo $sth; that the error goes away.

Edit: Try to do it this way:

$cnpj = "000001";
$db = new PDO(
    "mysql:host=host.com.br; dbname=databaseName",
    "usuario",
    "senha"
);
$db->query("INSERT INTO CLENTES (
    CNPJ
)
VALUES (
    ?
); ");
$result = db->query("Select * From CLENTES")->fetch(PDO::FETCH_ASSOC);
  • Not Lek, this has nothing to do, even though I take this echo thing I’ve done, it can’t enter data in the informed field

  • Look, I’m not saying it’s gonna work... echo $sth; is incorrect. Even if removing it does not enter the values, it is still wrong. You need to remove it.

  • this worked out thanks..

  • 1

    If his answer was the one that solved your problem, you should mark his as correct by clicking on the sign - As you have been previously guiding.

1

Try something this way since you just want to register.

$cnpj = "000001";
$sth = $lokos->prepare("INSERT INTO CLENTES (CNPJ) VALUES (?)"); 
$sth->execute(array($cnpj)); 

Remembering that your variable $lokos must be the one that connects to the database, normally used $conn something like.

1

Your logic is wrong...

$cnpj = "000001";
$sth = $lokos->prepare("INSERT INTO CLENTES (CNPJ) VALUES (?)"); 
$sth->bindParam(1, $cnpj, PDO::PARAM_STR);
$sth->execute();
// Até aqui está mais ou menos certo 

// O comando fetch serve para consultas, ou seja, SELECTs - ESTÁ ERRADO
$result = $sth->fetch(PDO::FETCH_ASSOC);
// Você não pode imprimir um objeto com echo
echo $sth;    

Do it this way:

$cnpj = "000001";
$sth = $lokos->prepare("INSERT INTO CLENTES (CNPJ) VALUES (:cnpj)"); 
$sth->bindParam(':cnpj', $cnpj, PDO::PARAM_STR);
$sth->execute();   

0

Object of class Pdostatement could not be converted to string

This error is caused by the line echo $sth;, $sth is not a scalar variable, to print objects or arrays use the functions print_r() or var_dump().

The insert normally does not return value, in which case remove the line

$result = $sth->fetch(PDO::FETCH_ASSOC);

To handle and find out the error in the query, add an if in execute()

if($sth->execute() === false){
   print_r($sth->errorInfo);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.