0

I am relatively new to PHP OOP and i know that there are numerous questions here on SO, but none of them seam to be pointing me in the right direction. I have created the class user, and I am calling this in another file.

I am trying to get the method 'reset' to call up 'connect', connect to the mysql db and then query it and set various properties to the row contents.

I am receiving no errors but for some reason it is not feeding the properties any data from the database.

I have tried placing the mySQL connect in the reset method, just to see if the variables cannot be passed between methods. But still no joy.

Can anyone point me in the right direction?

class user(){
  public function reset(){
    $this->connect();
    $sql ='SELECT * FROM users WHERE user_id="'.$user_id.'"' ;
    $result = mysqli_query($con,$sql);

    while($row = mysqli_fetch_array($result))
  {
      $this->user_name=$row['dtype'];
      $this->user_id=$row['user_id'];
      $this->atype=$row['atype'];
      $this->user_email=$row['user_email'];
      $this->group1=$row['group1'];
      $this->group2=$row['group2'];
      $this->group3=$row['group3'];
      $this->group4=$row['group4'];
      $this->group5=$row['group5'];
      $this->group6=$row['group6'];   
  }

            // Test that these properties are actually being echoed on initial file... it is
    //  $this->user_name = "john";  
    //  $this->user_email = "[email protected]";
    //  $this->dtype = "d";
    //  $this->atype = "f";
}

public function connect(){
            //GLOBALS DEFINED IN INDEX.PHP
        if ($db_open !== true){
        $con=mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);
    // Check connection
    if (mysqli_connect_errno())
      {
        $debug_system .= 'Error on user.php: ' . mysqli_connect_error().'<br\/>';
      } else {
        $db_open = true;
        $debug_system .= 'user.php: user details grab successful. <br\/>';
         }
}
}

}

1
  • Use $user = mysqli_fetch_object($result, 'user'); Commented Nov 5, 2013 at 11:19

2 Answers 2

3

If you are relatively new to PHP OOP, it is strongly recommended not to mess with awful mysqli API but learn quite sensible PDO first, and only then, making yourself familiar with either OOP and prepared statements, you may turn to mysqli.

Nevertheless, there shouldn't be no function connect() in the class user. You have to have a distinct db handling class, which instance have to be passed in constructor of user class

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

Comments

1

The problem lies in this line:

$sql ='SELECT * FROM users WHERE user_id="'.$user_id.'"' ;

At no point do you actually define $user_id. Presumably you actually mean $this->user_id.

$sql ='SELECT * FROM users WHERE user_id="'.$this->user_id.'"' ;

Better still would be to make full use of parameterized queries, which might look like this:

$sql ='SELECT * FROM users WHERE user_id=?' ;

You would then prepare the statement and bind the user ID, then execute the query:

$stmt = mysqli_prepare($sql);
mysqli_stmt_bind_param($stmt, $this->user_id);
mysqli_stmt_execute($stmt);

And then fetch the results:

while($row = mysqli_stmt_fetch($result))

As you can see, there is a whole load more to modern MySQL libraries. I'd advise you to do more research into how MySQLi and parameterized queries work (and perhaps PDO as well: it's a superior library) before you use them further. It will be worth the effort.

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.