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
– Adriano Luz
Since this starting, I recommend using PDO connection, because it is practical and safe.
– LucaoA
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
– LucaoA