3

Hi this is kind of an upgraded version of this question: query mysql database from inside a class The difference from the previous question, is i need a dynamic query not a static one or l$query = "SELECT col_1 FROM db.table"; So in order to have a dynamic query i need to use properties (or variables) so i can call different tables from that same class, or something like this "SELECT ‘$data’ FROM ‘$table’ ";

So far my class looks like this, similar to the previous question:

 $mysqli = new mysqli("localhost", "root", "", "intranetpugle");

class crudmum {

    private $table;
    private $data;
    private $mysqli;

  function __construct($mysqli) {   
    $this->mysqli = $mysqli;
  }


function runQuery($data2, $table2)
  {
    $this->table = $table2; $this->data = $data2;
    $query = "SELECT '$this->data' FROM '$this->table' ";

    $stmt = $this->mysqli->prepare($query);
    $stmt->execute();
    $stmt->bind_result($r);

    while($stmt->fetch())
    {    
          echo "<option>" . $r . "</option>";
    } 
  } 
};

This is how i run it:

$showme = new crudmum($mysqli); 
$showme->runQuery("priority", "trackboards" );

Note: When i dont use variables or properties inside the query or somethng like this, SELECT priority FROM trackboards, the query does work, only when i input the properties or variables (like the given example) it does not work.

I get this error: Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\devserv\i+d\bootstrap\functions.php on line 76

Anyone see what am i doing wrong, of course there is a mistake with the database query any ideas on how to query the database right in a dynamic way within a class, sorry new with OOP with PHP!

5
  • Can you echo your query "$query = "SELECT '$this->data' FROM '$this->table' ";" and exit the code so that we can check what is the issue. Commented Apr 15, 2016 at 18:58
  • Replace your select with the literal query SELECT priority FROM trackboards and see if it works. If it does, then there's something wrong with your properties. Commented Apr 15, 2016 at 19:07
  • Hi, SELECT priority FROM trackboards does work, only when i input the properties or variables it does not work Commented Apr 15, 2016 at 21:29
  • Are these separate files or one file? Commented Apr 17, 2016 at 8:56
  • Doesn't marke sense when using parameters the fatal error gets resolved and runQuery function returns some results. The Call to a member function prepare() on a non-object explicitly states that $this->mysqli is not a mysqli database resource. Checking with a literal query should make absolutely no difference occording to your supplied error. Commented Apr 17, 2016 at 9:51

1 Answer 1

1

found the mistake which was to add 'quotes' on the variables, like shown below:

 $query = "SELECT '$this->data' FROM '$this->table' ";

The correct way would be to take out those 'quotes' on the variables or like this:

$query = "SELECT $this->data FROM $this->table ";

With that fix, the query runs just fine, guess i lacked attention to detail, thanx everyone for their help.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.