php - receive returned value by class in oop

606

i have a class like this :

<?php
class connection {
    public $db;

    public function __construct() { 
    $this->db = new mysqli('localhost', 'root', '', '');

    if($this->db->connect_errno > 0){
        die('error ' . $this->db->connect_error);
    }
    $this->db->set_charset("utf8");
    }

}

class masssave extends connection {
    public $sql;
    public function insert_all {
    // do some works
            if ( $sql = $this->db->query("INSERT INTO $db_name ($str_db_value) values ($str_form_value_found) ")) {
            return true;    
            }
            else {
            return false;
            }
    }


}
?>

inmasssave class i seted$sql as public , now i want to use this class in some pages , like register page

$save = new masssave;
if ( $save->sql = true ) {
    echo 'ok';
} 
else {
    echo 'failed';
}

but the upper code is not working , it alwaysecho 'ok' even the query was failed ,

i also useif ( $save->sql == true ) but this code alwaysecho 'failed'

i am newbie in oop , but i think my php classes are ok , i think i am doing wrong way for checking returned value

405

Answer

Solution:

  1. replace$sql = $this->db->query with$this->sql = $this->db->query --$sql - local variable.$this->sql - object property
  2. call proper method after$save = new masssave; use$save->insert_all()
  3. use comparison (but not assigment)$save->sql = true - assigment,$save->sql == true - comparison. Assigment reutrns value of variable, so$save->sql = true is always true.
408

Answer

Solution:

This line should be...

if ( $save->insert_all() == true )

Because....First off, you are just checking if $save->sql is set, which will always echo true in your case.

Also you weren't even checking you were actually setting (= vs ==)

$save = new masssave;
if ( $save->sql = true ) 

Should be...

$save = new masssave;
if ( $save->sql == true ) {

But that will always return true anyways, because its just checking if the variable exists, which it does. What you really want is what I posted at the top. Because query is actually inside the insert_all function, which is where you are returning true or false.

903

Answer

Solution:

It will alwaysecho ok because you're setting the$save->sql totrue

if ( $save->sql = true ) {

The above is a big NO NO.

What you want is to compare it, so using== to compare the values

if ( $save->sql == true ) {

It would be simpler to just do:

if($save->sql) {
    echo 'ok;
} else {
    echo 'fail';
}

the aboveif check is basically sayingIF(TRUE) { SAY OK} ELSE { SAY FAIL}

People are also looking for solutions to the problem: php - String Replace Alphabet Ignoring HTML Tags and Elements

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.