我对闲置的客户和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()提供了无效的参数。我尝试了很多次,但无法解决。你能帮我吗?
答案 0 :(得分:0)
您也可以在自己的情况下使用此
$stmt = $pdo->query('SELECT name FROM users');
while ($row = $stmt->fetch())
{
echo $row['name'] . "\n";
}