How to return all fields of a "Mysql" line by the "id" of the line?

Asked

Viewed 1,685 times

-2

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

As it should be the query so that only by informing the ID, it returns all fields of the line ?

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

  • 5

    I’d just put the asteric (*)?

2 answers

5


Based on the comment, to display columns in html dynamically you can use the function array_keys() that returns the key names and with them do 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 proactive 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>';
  • I used the following code: $records = mysql_query("select * from paneeladm_usuarios"); foreach($records as $item){ $fields = array_keys($item); foreach($fields as $value){ echo $item[$value] . '<br>'; } } but an invalid argument error appears in foreach(), can you see my error? Warning: Invalid argument supplied for foreach()

  • @Magichat left a simple example because you did not put any code in the question but I can adapt the example for real use. $registros has the array structure returned by the database.

  • Mals ae, with this code that I put in the comment can already develop something? In the waiting...

  • @Magichat I edited the answer, now it comes out in table format is only set the header of the columns can use the same technique.

  • Perfect, now I will only adapt pro msqli... vlw x)

  • @Magichat that’s right :D

Show 1 more comment

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;
}

Creates the select

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

Gives fetchiada to display in HTML

$row = $res->fetch_assoc();

And scares everything in the little morals

    foreach($row as $echada_na_moral) {
        echo $echada_na_moral['id'];
        echo $echada_na_moral['nome'];
        echo $echada_na_moral['Sobrenome'];
}
  • 1

    So "Dom" , in this part of the scarf everything is the penalty for me, the line has several fields, in a way like:"While there are fields in this line of this ID, echa alone, automatic ? Get the problem? Thanks for the attention...

  • can only do with $echada_na_moral, but you can’t control the formatting of the fields two tips first: htmlspecialchars($echada_na_moral) will help you with html, the second is if you need it in JSON writes echo json_encode($Row)

Browser other questions tagged

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