Grab table id generated by setFetchMode(PDO::FETCH_ASSOC)

Asked

Viewed 178 times

0

Good afternoon friends, I am trying to generate a link sending a parameter that would be the row id of the table clicked

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

this code works on mysqli and mymsql smoothly, but in PDO I can’t find a way to "isolate" the foreach values because they are represented by v$. Follow the code (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 give a "get" in the Id or any of the columns separately since the foreach ta "docking" all the columns in the $v variable. (ps: I’m a beginner in php and Pdo, I don’t know if this is the default code to select because I copied from the mentioned site). Thank you all.

Edited --- Here another page that I am doing the same but WITHOUT THE PDO, it would be correct to use that same code on the page with PDO???

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";
}
  • $stmt->fetchAll() already has all the data (perId, perName,perPP) of elected. $result what’s it there for?

  • I honestly have no idea, I copied this code from the site w3school, I still don’t work with PDO so I wanted to know, is there another way more "sensible" or "common" to display data? Can I use the msqli pattern for example?

  • I edited the question to show how I am using in ANOTHER PAGE but without the PDO

No answers

Browser other questions tagged

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