Problems in a crud class

1

I'm trying to build a class to be used with mysql at first I have only two files so I'll paste the code I've already done here and explain my doubts.

classes/connect.php

<?php

/**
 * Created by PhpStorm.
 * User: evtns
 * Date: 08/06/2016
 * Time: 17:55
 */
class Database
{
    private $db_ip = '127.0.0.1';
    private $db_user = 'root';
    private $db_pass = 'aaxd31mubr';
    private $db_select = 'sistema';

    public function connet(){
        $dbc = mysqli_connect($this->db_ip,$this->db_user,$this->db_pass) or die("falha ao conectar");

        if(mysqli_select_db($dbc, $this->db_select ))
        {
            echo 'DB selecionado com sucesso <br />';
            return $dbc;
        }
        else{
            echo 'falhou';
            return false;
        }
    }

    public function __construct()
    {

    }

    public function select($cols='*',$tables ,$where=null, $value=null, $order=null)
    {
        $query = "SELECT $cols ";
        $query .="FROM $tables ";

        if(isset($where) and isset($value))
        {
            $query .="WHERE $where='$value'";
            echo 'teste 1 ok';
        }

        if(isset($order))
        {
            $query .="ORDER BY $order";
        }

        if($result_query = mysqli_query($this->connet(), $query)){
            return $result_query;

        }
        echo 'Falha ao executar querry da função select.';
    return false;
    }
}



index.php

<?php
/**
 * Created by PhpStorm.
 * User: evtns
 * Date: 08/06/2016
 * Time: 18:33
 */

include_once 'classes/connect.php';

$db = new Database();
//$cols='*',$tables,$where, $state//


$row = mysqli_fetch_array($db->select('*', 'estado'));

print_r(array_count_values($row));

?>

My problem is here I made an instance of the class, but when I try to access the elements of the result_query of public function select, with mysqli_fetch_array (), it only returns me the first element of the database, now if I try directly on the function it returns me all the values registered in the bank.

Does anyone know how to solve this problem?

    
asked by anonymous 11.06.2016 / 01:35

2 answers

1

I was able to solve the problem, I just created a variable to receive the value of the function and I used the variable to manipulate the data!

follow code example!

<?php
/**
 * Created by PhpStorm.
 * User: evtns
 * Date: 08/06/2016
 * Time: 18:33
 */

include_once 'classes/connect.php';

error_reporting(E_ALL);
$db = new Database();
//$cols='*',$tables,$where, $state//
$result = $db->select('*', 'tbl_clientes');

while($row = mysqli_fetch_array($result)){
    echo $row['id'] .' ID '. $row['nome'].'<br />';
}
?>
    
11.06.2016 / 22:02
1

The problem is with using the array_count_values The system ends up stopping because there are fields other than STRING or INTEGER.

Replace the end of index.php with the following code:

$db = new Database();
//$cols='*',$tables,$where, $state//


$row = mysqli_fetch_array($db->select('*', 'estado'));

echo "<pre>" . var_dump(($row));
    
11.06.2016 / 01:51