Portál AbcLinuxu, 12. května 2025 02:45
$stmt = $pdo->prepare($sql); $stmt->execute(array($email, $pass)); $id = $stmt->fetchAll(); if($stmt->rowCount() === 1) { $this->mess('Jste přihlášeni!'); foreach($id as $row) { echo $row['id']; } } else { $this->error('Zadané údaje jsou neplatné!'); }
Řešení dotazu:
fetchAll()
použít jen fetch()
nebo ještě lépe fetchColumn()
:
$stmt = $pdo->prepare($sql); $stmt->execute(array($email, $pass)); $id = $stmt->fetchColumn(); if ($id === false) throw new Exception('Zadané údaje jsou neplatné!'); $this->mess('Jste přihlášeni!'); echo $id;
$stmt = $pdo->prepare($sql); $stmt->execute(array($email, $pass)); $id = $stmt->fetchColumn(); $name = $stmt->fetchColumn(); if ($id === false OR $name === false) throw new Exception('Zadané údaje jsou neplatné!'); $this->mess('Jste přihlášeni!'); echo $id; echo $name;Děkuji
$stmt = $pdo->prepare($sql); $stmt->execute(array($email, $pass)); list($id, $name) = $stmt->fetch(); if ($id === false || $name === false) throw new Exception('Zadané údaje jsou neplatné!'); $this->mess('Jste přihlášeni!'); echo $id; echo $name;Pozor na "OR", slouží k jinému účelu a má jinou prioritu. V daném případě je však test
$name === false
zbytečný.
$row=reset($id); echo $row["id"];
list($ident, $name) = reset($id); echo "ident = $ident, name = $name\n";
echo $row['id'];
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.