Get Table Id generated by setFetchMode (PDO :: FETCH_ASSOC)

0

Good afternoon friends, I'm trying to generate a link by sending a parameter that would be the row ID of the clicked table

<a href="page?id=echo'.rows['id'].'">Editar</a>

This code works in mysqli and mymsql without any problems, but in the PDO I can not find a way to "isolate" foreach values because they are represented by v $. Follow the code (I copied from w3shool> PHP> Database> Select)

<body>

<?php
include_once("../conn.php");

echo "<table style='border: solid 1px black;'>";
echo "<tr><th>Id</th><th>Firstname</th><th>Partido</th></tr>";

class TableRows extends RecursiveIteratorIterator { 
function __construct($it) { 
    parent::__construct($it, self::LEAVES_ONLY); 
}

function current() {
    return "<td style='width:auto;border:1px solid black;'>" . parent::current(). "</td>";
}

function beginChildren() { 
    echo "<tr>"; 
} 

function endChildren() { 

    echo "<td style='border: 1px solid red;'><a href='senadoresTeste.php'>Editar</a></td>";
    echo "</tr>" . "\n";
} 
} 

try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT perId, perName,perPP FROM elected"); 
$stmt->execute();

// set the resulting array to associative
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC); 
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) { 
    echo $v;
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
echo "</table>";
?>

The question is whether it is possible to get a "get" in the Id or any of the columns separately since the foreach is "coupling" all the columns in the $ v variable. (ps: I am new to php and pdo, I do not know if this is the default code for select because I copied the site). Thank you all.

Edited --- Here's the other page that I'm doing the same but WITHOUT PDO, would it be correct to use that same code on the PDO page ???

if ($result->num_rows > 0) {
echo "<table><tr><th>ID</th><th>Name</th><th>Partido</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
    echo "<tr><td>".$row["perId"]."</td><td>".$row["perName"]."</td><td> ".$row["perPP"]."</td><td><a href='teste.php?id=echo".$row["perId"]."'>Editar</a></td></tr>";
}
 echo "</table>";
} else {
echo "0 results";
}
    
asked by anonymous 28.05.2016 / 21:44

0 answers