开发者

Query string not handled from child class

开发者 https://www.devze.com 2023-04-12 00:35 出处:网络
This is my first time here so i hope you can help me. This is my problem: I\'m busy with a log-in script in oop.

This is my first time here so i hope you can help me. This is my problem:

I'm busy with a log-in script in oop.

I have the following classes ready:

  • database class

    this is just an connection/disconnection class

  • query class extends the database class

    there is only 1 function and that only takes a query string and get it through the database and eventually returns an array with the data.

  • login class

    this class will get user login information and gets user informatie from the database.

this is the folder structure:

/
/cgi-bin -> holds the database and query class
/libraries -> holds the login class

Then there are 2 files left and they are the index.php and the global.php.

In my index I have this:

require_once('global.php');

print_r($login->userCheck('test'));

and this is inside my global.php:

include('cgi-bin/database.php');
include('cgi-bin/query.php');
include('libraries/login.lib.php');


$login = new Login();

this is my login class

class Login extends Query{

  public function Login(){

  }

  public function userCheck($userCredentials){
  $result = $this->qString('SELECT * FROM users');
  return $result;

}

}

and my query class

class Query extends Database{
  function qString($qString){

    //Start connection with database
    $this->conncect();

    $result = $this->db->query($qString);
    // fetch associative array
    while ($row = mysqli_fetch_assoc($result)) {
      $data[] = $row;
    }

    // free result set 
    mysqli_free_result($result);

    //Close connection with database
    $this->disconnect();

    //Check mysqli connection
    //print_r(explode('  ', mysqli_stat($this->db)));

    return $data; 

  }
}

and the datab开发者_如何学Pythonase class:

class Database {

  //Private variables for database connection.
  private $server;
  private $usern;
  private $userp;
  private $database;

  //The database object.
  protected $db;

  function Database(){

    $dbCredentials = explode(',',file_get_contents('cgi-bin/dbcredentials.txt'));

    $this->server = $dbCredentials[0];
    $this->usern  = $dbCredentials[1];
    $this->userp  = $dbCredentials[2];
    $this->db     = $dbCredentials[3];

  }

  protected function conncect(){            
    $this->db = mysqli_connect($this->server, $this->usern, $this->userp, $this->db);
  }

  protected function disconnect(){
    mysqli_close($this->db);
  }

}   

now when I run this it says this:

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\Login\cgi-bin\query.php on line 11

Warning: mysqli_free_result() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\Login\cgi-bin\query.php on line 16

Notice: Undefined variable: data in C:\xampp\htdocs\Login\cgi-bin\query.php on line 25

Why am I getting this error?

Edit 12-10-2011:

I have found what the error was.

just to share it with you: There error was that the construct from the database class was never run.

Because of that, the connection details like username and password were never set to the private variables and with that it could never connect.

With the result that the query never could run from the login class.

So it was pretty simple in the end.


You need to read the documentation for mysqli::query. It returns a result set only when there are results. On error, it will return boolean false, and for many types of queries it will return a boolean true on success:

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a MySQLi_Result object. For other successful queries mysqli_query() will return TRUE.

You're also not checking the return value of $this->db->query(...). If it is failing and returning false, you're blindly passing that boolean value to mysqli_fetch_assoc, hence the error message

mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given

Please add some basic error handling to your code:

$result = $this->db->query($qString);

if ($result === false) {
  // error in query
  die("'$query' failed");
}

if ($result === true) {
  // not a query which returns results
  return true;
}

// fetch associative array
while ($row = mysqli_fetch_assoc($result)) {
  $data[] = $row;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号