2

I am having a public function doing query for mysql. Everything works great in my local computer but having trouble once it was uploaded to the godaddy server. Basically, the $rows is null when I called the method.

I am sure the database data are correct and the query should return few records. I have spent hours trying to figure it out but have no luck. I was hoping you guys can help me out on this one. Thanks in advance.

Code

    class test{

     public function getEmployees($username,$password) {



         $stmt = mysqli_prepare($this->connection,
              "SELECT name, password
               FROM employee
               WHERE name='$username' && password='$password'
               ");     

          $this->throwExceptionOnError();

          mysqli_stmt_execute($stmt);
          $this->throwExceptionOnError();

          $rows = array();


          while (mysqli_stmt_fetch($stmt)) {
              $rows[] = $row;
              $row = new stdClass();
              mysqli_stmt_bind_result($stmt,  $row->name, $row->password);
          }
          if(mysqli_stmt_num_rows($stmt)==0){

              return false;
          }else{
              return $rows;
          }



          mysqli_stmt_free_result($stmt);
          mysqli_close($this->connection);
   }

}

$test=new test();
$row=$test->getEmployees('bob','1111');
echo "<pre>";
print_r($row);
echo "</pre>";

//it prints nothing on the page
2
  • remove this line $row = new stdClass(); and try Commented May 3, 2012 at 3:18
  • Thanks guys, I just figured it out. I had to change mysqli_stmt_num_rows($stmt)==0 to mysqli_stmt_num_rows($stmt). Not sure why though. Commented May 3, 2012 at 3:31

1 Answer 1

1

I think the problem is code structure. Try:

class test{

     public function getEmployees($username,$password) {



         $stmt = mysqli_prepare($this->connection,
              "SELECT name, password
               FROM employee
               WHERE name='$username' && password='$password'
               ");     

          $this->throwExceptionOnError();

          mysqli_stmt_execute($stmt);
          $this->throwExceptionOnError();

          $rows = array();
          $row = new stdClass();
          mysqli_stmt_bind_result($stmt,  $row->name, $row->password);

          while (mysqli_stmt_fetch($stmt)) {
              $rows[] = $row;

          }

          mysqli_stmt_free_result($stmt);
          mysqli_close($this->connection);
          $temp = mysqli_stmt_num_rows($stmt); 
          if($temp==0){

              return false;
          }else{
              return $rows;
          }



   }

}

$test=new test();
$row=$test->getEmployees('bob','1111');
echo "<pre>";
print_r($row);
echo "</pre>";
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks man. Please see my comment. Your code would work for me too. I will give u green. :D

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.