PHP Fatal error: Call to a member function initBD () on a non-object

0

Hello, I have the following php code

function updateRecord($email, $score){  

$sql = "UPDATE record set record = $score where email = '$email' ";
$banco-> initBD()->exec($sql); //O Log indica o erro nessa linha

if($banco->rowCount()>0){
    $obj = array("resposta"=>1);
}else{
    $obj = array("resposta"=>2);
}
    $array[] = $obj;

    // json_encode($array);
    echo stripslashes( json_encode($array));
}

function initBD(){
    //servidor online
    $opcoes = array(PDO::MYSQL_ATTR_INIT_COMMAND=> 'SET NAMES UTF8');
    return (new \PDO ('mysql:host=localhost;port=80;dbname=yyyyyy; charset=utf8', 'login', 'senha', $opcoes));
}

I do not know if this is enough to answer the question, but I'm waiting for feedback.

    
asked by anonymous 17.04.2016 / 03:25

1 answer

0

Well, good practice would not be the best way for a DML and connection structure, but based on your code and some modification, SEE: NOTE: MySQL port equal to 80? It would not be 3306 or any other than port 80.

    <?php

function updateRecord($email, $score, $database_handle){

$sql = "UPDATE record set record = $score where email = '$email' ";
$count = $database_handle->exec($sql);


if($count > 0){
    $obj = array("resposta"=>1);
}else{
    $obj = array("resposta"=>2);
}
    $array[] = $obj;

    // json_encode($array);
  echo stripslashes( json_encode($array));
}

function initBD(){
    //servidor online
    $opcoes = array(PDO::MYSQL_ATTR_INIT_COMMAND=> 'SET NAMES UTF8');
    $database_handle = new PDO('mysql:host=localhost;port=3306;dbname=yyyyyy; charset=utf8', 'login', 'senha', $opcoes);
    return $database_handle;
}
$database_handle = initBD();
updateRecord(email,score, $database_handle);
    
17.04.2016 / 15:44