Uncaught Error: Call to a Member Function prepare() on array

Asked

Viewed 269 times

0

Environment: php 7.4.4 and Apache24 on a Microsoft Server R2 2012. @EDIT: MS Sql Server Database.

Guys I’m getting the following error:

Fatal error: Uncaught Error: Call to a member function prepare() on array in C:\Apache24\htdocs\teste\backend\dist\fx.php:564 Stack trace: #0 C:\Apache24\htdocs\teste\backend\dist\conn.php(52): selectSQL('VW_FATOVENDA', Array, 'WHERE 1 = 1 AND...', Array, Array) #1 {main} thrown in C:\Apache24\htdocs\cns_metronic\backend\dist\fx.php on line 564

Accessing the informed line, I have the following function:

function selectSQL($table, $columns, $where, $parameters, $pdo){

  $querySQL = "SELECT ".implode(", ", $columns)." FROM ".$table." ".$where." ";

  $query = $pdo->prepare($querySQL);

  if(is_array($parameters) && count($parameters) > 0){
    foreach ($parameters as $key => $value) {
      $query->bindValue(($key + 1), $value);
    }
  }
  $query->execute();
  $returnValues = $query->fetchAll();

  return $returnValues;
}

@EDIT:

Also informing the query that is mounted.

$table = 'VW_FATOVENDA';
$columns = array('Vendedor', 'DataFaturamento', 'SUM(Subtotal) as Subtotal');
$where = "WHERE 1 = 1 AND CONVERT(DATE, DataFaturamento, 103) BETWEEN CONVERT(DATE, '01/12/2019', 103) AND CONVERT(DATE, '10/12/2019', 103)  AND TipoMovimento IN ('VENDA', 'NOTA MÃE')
    GROUP BY Vendedor, DataFaturamento, Dia, Ano
    ORDER BY dia ASC";
$parameters = array();
$pdo = array();

$graficoSQL = selectSQL($table, $columns, $where, $parameters, $pdo);

Remembering that the connection parameters are these:

date_default_timezone_set('America/Sao_Paulo');

$host_bd = 'localhost';
$username_bd = 'root';
$password_bd = 'root123';
$nome_bd = 'Base_Testes';

try{
  $pdo = new PDO('sqlsrv:Server='.$host_bd.';Database='.$nome_bd, $username_bd, $password_bd);
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

}catch(PDOException $e){
  //finalDialogue(2, "Erro no acesso ao banco de dados.", null);
  echo 'Erro no acesso ao banco de dados:<br />' . $e->getMessage();
}

I looked for several ways to try to solve this problem but I always fall in the same error message, I’ve also reviewed the Extensions and all are in the "ext" folder properly initialized without any kind of error. Detail, on local computer worked but on the server does not work.

  • Of course it will give this error, you declare $pdo = array(); and then $graficoSQL = selectSQL($table, $columns, $where, $parameters, $pdo);

  • Thank you for your observation!

  • Thanks nothing! It’s $ 50,00 for the information and more $ 50,00 for calling me uncle without authorization.

No answers

Browser other questions tagged

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