How to return all the fields of a "Mysql" line with the "id" of the line? [closed]

-2

Given a table "Mysql", where there are 3 fields: ID - First Name - Last Name

How should the query be so that just by entering the ID, does it return all fields in the row?

Note: I put only 3 fields for example, but the query should serve a row that has many fields.

    
asked by anonymous 17.05.2016 / 17:23

2 answers

5

Based on the comment, to display the columns in html dynamically you can use the array_keys() function that returns the key names and with them make a second foreach to display the values.

$registros =[
        ['id' => 1, 'nome' => 'alberto'],
        ['id' => 2, 'nome' => 'beto'],
        ['id' => 3, 'nome' => 'carlos'],

];

foreach($registros as $item){
    $campos = array_keys($item);
    foreach($campos as $valor){
        echo $item[$valor] .'<br>';
    }
}

A more proxi- mate example of reality would be the code below, it can be abstracted to function.

$query = mysql_query("select * from paineladm_usuarios") or die(mysql_error());

echo '<table border="1">';
while($row = mysql_fetch_assoc($query)){
    $campos = array_keys($row);
    echo '<tr>';
    foreach($campos as $campo){
        echo '<td>'. $row[$campo] .'</td>';
    }
    echo '</tr>';
}
echo '</table>';
    
17.05.2016 / 19:34
3

First make the connection by starting a Mysqli Object

$mysqli = new mysqli("example.com", "user", "password", "database");
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: " . $mysqli->connect_error;
}

Create select

$res = $mysqli->query("SELECT * From tabela where ID = 'Coloca o ID aqui'");

Give the fetch to be able to show in HTML

$row = $res->fetch_assoc();

And throw it all in the morality

    foreach($row as $echada_na_moral) {
        echo $echada_na_moral['id'];
        echo $echada_na_moral['nome'];
        echo $echada_na_moral['Sobrenome'];
}
    
17.05.2016 / 17:40