Home > Back-end >  How Show result Query in textarea
How Show result Query in textarea

Time:09-26

I have data tabel vendor:

ID vendor Name
1 AAA
2 bbb

with query

$query=mysql_query("SELECT * FROM `vendor`)

how show results all data in textarea html like this

<Textarea>
1-AAA
2-bbb
</textarea>

CodePudding user response:

You can do something like this:

<?php

// ... database connection here

$result = mysql_query("SELECT * FROM vendor");

?>
<textarea>
    <?php
        while ($row = mysql_fetch_assoc($result)) {
            echo $row['ID'], '-', $row['vendor_Name'], ' ';
        }
    ?>
</textarea>

Also consider using PDO instead of mysql_* functions, because they are deprecated and removed in PHP > 5.5.

Same solution with PDO:

<?php

try {
    $pdo = new PDO("mysql:host=...;dbname=...", "username", "password");
} catch (PDOException $e) {
    die($e->getMessage());
}

$stmt = $pdo->query("SELECT * FROM vendor");

?>
<textarea>
    <?php
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            echo $row['ID'], '-', $row['vendor_Name'], ' ';
        }
    ?>
</textarea>
  • Related