Regen name of columns and values of MySQL

1

I want to get the name of the columns of a table and their respective records .. Ex: I have the table registers and it has the columns name, login, password and there are 2 records recorded in this table, getting + - like this:

name = alisson / login = admin / password = 123

name = alisson2 / login = admin2 / password = 123

Then I would like to take the name of the columns ( name, login, password ) and get the value of the 2 records that it has in it, 123 )

<?php

protected function ListColumn($table){

        $Query = $this->connect->prepare("SHOW COLUMNS FROM {$table}");
        $Query->execute();

        while($e = $Query->fetch(PDO::FETCH_ASSOC)){

            $colunas[] = $e['Field'];

        }

        return array($colunas);
    }


QuerySearch = $this->connect->prepare("SELECT * FROM {$table} WHERE login LIKE '%teste%'");
        $QuerySearch->execute();

        $Retornados = $QuerySearch->rowCount();

        if($Retornados > 0){

            while($b = $QuerySearch->fetch(PDO::FETCH_ASSOC)){

                foreach($this->ListColumn($table) as $field){               

                    $s[] = $field;
                }

            }

            echo json_encode($s);

        }else{

            echo json_encode(array('error'=>'Nada foi encontrado com o termo informado.', 'result'=>'0'));
        }
?>
    
asked by anonymous 31.07.2014 / 10:23

1 answer

6

Well, I may be wrong about your question, but from what I understand, you want to make an inquiry and return all the fields of the table and make the listing without the definition of the field in $row

  

use $row[$field] instead of $row['ID']

If it's really that, my suggestion is simple foreach :

$pdo = new \PDO( 'mysql:host=localhost;dbname=xxx' , 'xxx' , 'xxx' );
$stmt = $pdo-> prepare( 'select * from table' );
if( $stmt-> execute() )
{
    while( $row = $stmt-> fetch( \PDO::FETCH_ASSOC ) )
    {
        foreach( $row as $field => $value )
        {
            echo 'my field: ' . $field . ' - ' . $value;
        }
    }
}

In this way you have $campo and $valor

    
31.07.2014 / 11:25