Problems in a crud class

Asked

Viewed 63 times

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 doubt.

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 trying to access the elements of the public Function select result_query, with mysqli_fetch_array(), it only returns me the first element of the database, now if you try to direct in the function it returns me all values registered in the database.

someone knows how to solve this problem?

  • the database will return you an array of results, you will need a repeat structure to navigate through them

  • Since this starting, I recommend using PDO connection, because it is practical and safe.

  • Follow on my github a class that facilitates communication with PDO. It even has methods that facilitate insertion/editing => https://github.com/LucaoA/connectionPDO/blob/master/Connection.php

2 answers

1

The problem is using the function array_count_values The system ends up stopping because there are fields that are not 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));
  • $Row = mysqli_fetch_array($db->select('', 'tbl_clients')); for($i=0; $i<10; ++$i){ echo 'ID' . $i . $Row['id'] . '<br />'; } I did this way now and I still get only the first value of the database. now if I leave the above code in the function below that is inside the class database it prints all the values of the existing database! $Row = mysqli_fetch_array($db->select('', 'state'));

1


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

follows example of code!

<?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 />';
}
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.