为php和其他客户端上的foreach()提供了无效的参数

时间:2018-12-21 07:51:54

标签: php

我对闲置的客户和pdo有疑问。我想按单个ID获取用户信息,这是我的代码:

 require_once('index.php');
 $action=$_POST["action"];
 $id= $_POST["id"];
 switch ($action)
 {
      case 'GETCLIENTS':
           echo 'clientlar çekildi.';
           $stmt = $myPDO->query("SELECT * FROM users",PDO::FETCH_ASSOC);
           foreach ($stmt as $row) {
                echo $row['first_name']."-".$row['last_name']."<br />\n";
           }
      break;
      case 'GETCLIENT':
           echo 'client çekildi.';
           $stmt = $myPDO->query("SELECT * FROM users WHERE id=".$id."",PDO::FETCH_ASSOC);
           foreach ($stmt as $row) {
                echo $row['first_name']."-".$row['last_name']."<br />\n";
           }
      break;
 }

但是它说我为foreach()提供了无效的参数。我尝试了很多次,但无法解决。你能帮我吗?

1 个答案:

答案 0 :(得分:0)

您也可以在自己的情况下使用此

$stmt = $pdo->query('SELECT name FROM users');
while ($row = $stmt->fetch())
{
    echo $row['name'] . "\n";
}