PDO bindValue PHP

Asked

Viewed 295 times

0

I’m studying PHP and I’m seeing object oriented PDO but I realized a connection already tested, however the bindValue returns error at time of execution, this error:

Fatal error: Uncaught Error: Call to Undefined method PDO::bindValue() in C: wamp64 www test class.php on line 27

Code:

<?php
	

	class funcionario{

		private $con;
		private $nome;
		private $cnpj;

		public function __construct(){
			require 'conexao.php';
			$this->con = $pdo;
		}

		  public function __set($atrib, $value){
          $this->$atrib = $value;
      }
 
      public function __get($atrib){
          return $this->$atrib;
      }

		public function cadastrarFuncionario(){
			$cmd = $this->con;

			$cmd->prepare("INSERT INTO fornecedor(nome, cnpj) VALUES (:nome, :cnpj");
			$cmd->bindValue( ':nome', $this->nome );
			$cmd->bindValue( ':cnpj', $this->cnpj );

			$resultado = $cmd->execute();

			if (resultado){
				echo "Dados armazenados!";
			}else{
				echo "Dados Não Armazenados!";
			}



		}


	}

?>

1 answer

2


bindParam() and bindValue() are methods of Pdostatement that in this context is the return of prepare. Do a test with this model I sent!

<?php


    class funcionario{

        private $con;
        private $nome;
        private $cnpj;

        public function __construct(){
            require 'conexao.php';
            $this->con = $pdo;
        }

          public function __set($atrib, $value){
          $this->$atrib = $value;
      }

      public function __get($atrib){
          return $this->$atrib;
      }

        public function cadastrarFuncionario(){
            $cmd = $this->con;

            $st = $cmd->prepare("INSERT INTO fornecedor(nome, cnpj) VALUES (:nome, :cnpj");
            $st->bindValue( ':nome', $this->nome );
            $st->bindValue( ':cnpj', $this->cnpj );

            $resultado = $st->execute();

            if (resultado){
                echo "Dados armazenados!";
            }else{
                echo "Dados Não Armazenados!";
            }



        }


    }

?>
  • 1

    Actually the error is only the lack of a parenthesis at the end of the query. You need to close the VALUES parenthesis inside the quotes.

  • 1

    had this Pdostatement error and had tmb this problem in query

Browser other questions tagged

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