Ciprian
Ciprian

Reputation: 3226

Simple mySQLi select to an array

Building from a tutorial I found online.

I m trying to select all items from the 'items' table and create an array. Not sure how this is suppose to work. This $result = $this->connection->query($q); is what is causing the problem.

<?php
//DB.class.php

class DB {

    protected $db_name = 'dbname';
    protected $db_user = 'user';
    protected $db_pass = 'pass';
    protected $db_host = 'localhost';
    protected $connection;

    public function connect() {
    $connection = new mysqli($this->db_host, $this->db_user, $this->db_pass, $this->db_name);

    // check connection
    if ($connection->connect_error) {
      trigger_error('Database connection failed: '  . $connection->connect_error, E_USER_ERROR);
    }

    }

  public function resultToArray($result) {
    $rows = array();
    while($row = $result->fetch_assoc()) {
        $rows[] = $row;
    }
    return $rows;
  }

  public function sel($table) {
    $q = "SELECT * FROM $table";
    $result = $this->connection->query($q);
    $rows = $this->resultToArray($result);

    return $rows;

    $result->free();

  }

}

Upvotes: 1

Views: 287

Answers (2)

Kevin
Kevin

Reputation: 41885

I think you should set a constructor, but if you don't want, just return an instance of it first and set your $this->connection property instead of $connection (the normal variable):

class DB {

    protected $db_name = 'test';
    protected $db_user = 'test';
    protected $db_pass = 'test';
    protected $db_host = 'localhost';
    protected $connection;

    public function connect() {
        $this->connection = new mysqli($this->db_host, $this->db_user, $this->db_pass, $this->db_name);
        // ^^ this one, not $connection
    // check connection

    if ($this->connection->connect_error) {
      trigger_error('Database connection failed: '  . $connection->connect_error, E_USER_ERROR);
    }

        return $this->connection; // then return this
    }

  public function resultToArray($result) {
    $rows = array();
    while($row = $result->fetch_assoc()) {
        $rows[] = $row;
    }
    return $rows;
  }

  public function sel($table) {
    $q = "SELECT * FROM $table";

    $result = $this->connection->query($q);
                  // ^ so that if you call this, you have the mysqli object
    $rows = $this->resultToArray($result);

    return $rows;

    $result->free();

  }

}

$db = new DB(); // instantite,
$db->connect(); // then connect, shouldn't have to have this if you put the connection automatically on construct
$result = $db->sel('users'); // feed a valid existing table name
echo '<pre>';
print_r($result);

Upvotes: 0

truesource
truesource

Reputation: 397

make a construct function like

public $mysqli;
    public function __construct()
    {
        $mysqli = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
        $this->mysqli = $mysqli;
    }

public function sel($table,$whr)
    {
    $query = "SELECT * FROM ".$table." where id='$whr'";
    $result = $this->mysqli->query($query);
    $total = array();
    while($row = $result->fetch_assoc()){
            //print_r($row);die;
         $total[] = $row;
    }//print_r($total);die;
    return $total;
    }

Upvotes: 1

Related Questions