Regastar Mysql column name and values

Asked

Viewed 6,313 times

1

I want to get the name of the columns of a table and their respective records.. Ex: I have the table criminal records and on it are the columns name, login, password and there are 2 records recorded in this table, leaving +- so:

name = Alisson / login=admin / password = 123

name = alisson2 / login=admin2 / password = 123

So I’d like to take the name of the columns (name, login, password,) and take the value of the 2 records in it (Alisson, admin, 123 e alisson2, admin2, 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'));
        }
?>
  • It was not very clear your doubt...

  • @Papacharlie edited the question

  • 1

    I’m trying to understand but I can’t... What is the relationship with the names of the columns? In your code, which row executes what you want to do?

  • I just want to get the column names of a table and along with the name, get the records

  • You get the names of the tables and then the records.

  • I even know how to do with mysql_fetch_array, but I won’t inform for example that I want the names, making for ex $data["name"]... the "name" will be the result of capturing the columns to form a $data[$field]

  • @Alisson keep using PDO , these on the right track. I believe the Pope’s answer is what you want

  • Too bad I only saw the comments two years later :) Follow the version with mysqli, more suitable for visitors who do not need "portability" of DB: How to list a query without knowing what will be returned?

Show 3 more comments

1 answer

6


Well, I may be wrong about your doubt, but from what I understand, you want to make a query and return all fields of the table and do the listing without setting the field on $row

use $row[$field] in place of $row['ID']


If that’s true, my suggestion is a 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;
        }
    }
}

This way you have $campo and $valor

Browser other questions tagged

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