How to take select(MYSQL) data and display in different fields?

Asked

Viewed 801 times

0

Good morning!! The B.O is the following:

I have my select in php where I take the table data, I wanted to know how to have this data separated and displayed individually.

Ex: SELECT nome_cliente, idade_cliente From tb_cliente WHERE COD_cliente={$x}

This query will return me only one line, so wanted to catch these

results and put each one in a variable so that it can catch the name and

put in the value of one input and age in the value of another.

1 answer

0


I recommend connecting to Mysql using the PDO class, as an example below:

<?php

$server = "localhost";
$database = "test";
$user = "usuario";
$pass = "senha";

try {
    $dbh = new PDO('mysql:host='. $server . ';dbname=' . $database, $user, $pass);
    foreach($dbh->query('SELECT nome_cliente, idade_cliente From tb_cliente WHERE COD_cliente={$x}') as $row) {
        echo '<input type="text" name="nome" value="' . $row["nome_cliente"] . '"><br>';
        echo '<input type="text" name="nome" value="' . $row["idade_cliente"] . '"><br>';
    }
    $dbh = null;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

Browser other questions tagged

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