I'm writing a simple db infrastructure. select function suppose to perform a simple query and returns a result set.
For some reason, I am not getting any results back.
What should I do in order to fix my code?
protected $connection;
public function connect() {
// Try and connect to the database
if(!isset($this -> connection)) {
// Load configuration as an array. Use the actual location of your configuration file
$config = parse_ini_file('config.ini');
$this -> connection = new mysqli('localhost',$config['username'],$config['password'],$config['dbname']);
}
// If connection was not successful, handle the error
if($this -> connection === false) {
// Handle error - notify administrator, log to a file, show an error screen, etc.
return false;
}
return $this -> connection;
}
public function select($query) {
$connection = $this -> connect();
$stmt = $connection->prepare($query);
$stmt->execute();
$stmt->store_result();
$result = $stmt->get_result();
$rows = array();
if($result === false) {
return false;
}
while ($row = $result -> fetch_assoc()) {
$rows[] = $row;
}
$stmt->free_result();
$stmt->close();
$connection->next_result();
return $rows;
}
Execution:
$result = $db -> select('SELECT 1');
echo $result;
Aucun commentaire:
Enregistrer un commentaire