PHP Fatal error: Call to a Member Function initBD() on a non-object

Asked

Viewed 76 times

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 don’t know if that’s enough to answer the question, but I’m waiting for the feedback.

  • It is not enough, where is being instantiated the object "bank" and initdb is a method of that object? apparently it is a function only

  • tries to trade "$bank-> initBD()->exec($sql);" for only "initBD()->exec($sql);"

  • Who executes is a library in Volley. However, Log indicates that the json was sent, however the error happens only in this part. I’ll try the dirt.

  • " Call to a Member Function initBD() on a non-object", that is $bank is not an object, but, still you are trying to access a method from it. So I asked him where he’s being instantiated, but he’s actually not. I also noticed that in your code, there is a function with the same name as the invoked method. It was the code of this function that you tried to use on the line that gave problem?

  • yes so I only added the two functions to the question, the line indicating the method call and the Initbd method()

  • Then it would be better to separate and specify. In the first section "Line that failed:" and "Method called:". But as I said, the method called is irrelevant. $bank is not an object. It shows the chunk where $bank is being set (Receiving an instance, or anything). I also recommend using: http://php.net/manual/en/function.gettype.php and pass $bank as parameter, and see the answer.

Show 1 more comment

1 answer

0


Well, for good practices it would not be the best way for a connection structure and DML, but based on its code and with some modifications, SEE: NOTE: Mysql port equal to 80? Wouldn’t be 3306 or 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);

Browser other questions tagged

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