PHP-试图将Mysqli转换为PDO

问题描述:

I am trying to convert the Mysqli code to use PDO

Mysqli code looks like the following (which works great)

$rs = "SELECT * FROM team";

$result = mysqli_query($con,$rs);           
$data = mysqli_num_rows($result);
$responses = array();
    if($data != 0) {
        while($results = mysqli_fetch_assoc($result))
          {
        echo "<tr><td>".$results['code'] ."</td>";
        echo "<td>".$results['username'] ."</td>";                   
        }

My PDO code I tried

    $stmt = $con->prepare("select * from  team");
    $stmt->execute();
    if($stmt->rowCount() > 0)
 $result = $stmt->setFetchMode(PDO::FETCH_ASSOC); 

How should I write a while loop here

on w3schools website, the information given to retrieve the records using PDO is as below, which did not say what is V and doesn't say how do I retrieve the fields code and username from the table.

  foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) { 
        echo $v;
    }

我正在尝试将Mysqli代码转换为使用PDO p>

Mysqli代码 看起来如下(效果很好) p>

  $ rs =“SELECT * FROM team”; 
 
 $ result = mysqli_query($ con,$ rs);  
 $ data = mysqli_num_rows($ result); 
 $ responses = array(); 
 if($ data!= 0){
 while($ results = mysqli_fetch_assoc($ result))
 {
 \ echo  “&lt; tr&gt;&lt; td&gt;”。$ results ['code']。“&lt; / td&gt;”; 
 echo“&lt; td&gt;”。$ results ['username']。“&lt; / td&gt;  ;“;  
} 
  code>  pre> 
 
 

我试过的我的PDO代码 p>

  $ stmt = $  con> gt;准备(“select * from team”); 
 $ stmt-&gt; execute(); 
 if($ stmt-&gt; rowCount()&gt; 0)
 $ result = $ stmt-&gt  ;调用setFetchMode(PDO :: FETCH_ASSOC);  
  code>  pre> 
 
 

我应该如何在w3schools网站上写一个while循环 p>

,使用PDO检索记录的信息是 如下所示,它没有说什么是V,也没有说我如何从表中检索字段 code code>和 username code>。 p>

  foreach(new TableRows(new RecursiveArrayIterator($ stmt-&gt; fetchAll()))$ k =&gt; $ v){
 echo $ v; 
  } 
  code>  pre> 
  div>

That's a horrifically awful way to select data from a database. It's far more complex than is necessary. I suppose it might be useful in a certain context, but not here.

The simple way is with PDOStatement::fetch. This works in much the same way as mysqli_fetch_assoc. (You don't strictly speaking need to check the row count, though you might have other code if there are no results.)

while ($row = $stmt->fetch()) {
    echo "<tr><td>".$row['code'] ."</td>";
    echo "<td>".$row['username'] ."</td>";                   
}

My preferred way, however, is with PDOStatement::bindColumn, which gets rid of arrays and uses nice plain variables instead:

$stmt->setFetchMode(PDO::FETCH_BOUND);
$stmt->bindColumn('code', $code);
$stmt->bindColumn('username', $username);
while ($row = $stmt->fetch()) {
    echo "<tr><td>$code</td>";
    echo "<td>$username</td>";                   
}

$stmt = $con->prepare("select * from  team");
$stmt->execute();
if($stmt->rowCount() > 0)
{
  while($row = $stmt->fetch(PDO::FETCH_ASSOC))
  {
    echo "<tr><td>".$row['code'] ."</td>";
    echo "<td>".$row['username'] ."</td>"; 
  }
}

It's PDO, so everything is MUCH simpler:

foreach ($con->query("SELECT * FROM team") as $results) {
    echo "<tr><td>".$results['code'] ."</td>";
    echo "<td>".$results['username'] ."</td>";                   
}

$rs = "SELECT * FROM team";
$stmt = $pdo->query($rs );
while ($row = $stmt->fetch())
{
    echo "<tr><td>".$row ['code'] ."</td>";
    echo "<td>".$row ['username'] ."</td></tr>";  
}

or

$stmt = $pdo->query($rs );
foreach ($stmt as $row)
{
    echo "<tr><td>".$row ['code'] ."</td>";
    echo "<td>".$row ['username'] ."</td></tr>"; 
}

or

$data = $pdo->query($rs )->fetchAll();
foreach ($data as $row)
{
    echo "<tr><td>".$row ['code'] ."</td>";
    echo "<td>".$row ['username'] ."</td></tr>"; 
}