gpt4 book ai didi

php - fetchAll() 没有结果;

转载 作者:行者123 更新时间:2023-11-29 08:08:47 24 4
gpt4 key购买 nike

我完全是 PHP 和 MySQL 领域的新手。我正在读 Kevin Yank 的书,在做他的一个例子时,我遇到了一个奇怪的结果。我确信我遵循并正确输入了他书中写的代码,但我想知道为什么我没有得到相同的结果。我来回检查了代码并确定它是正确的,或者也许我遗漏了一些东西。

我在这里发布这个是因为我知道这里的很多内容对像我这样的初学者非常有帮助并且非常友善。任何意见都将受到高度赞赏。期待评论,以便我可以继续我的学习,因为我认为我因为这个奇怪的错误而陷入困境。

请参阅以下代码供您引用。

index.html

    <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Joke CMS</title>
</head>
<body>
<h1>Joke Management System</h1>
<ul>
<li><a href="jokes/">Manage Jokes</a></li>
<li><a href="authors/">Manage Authors</a></li>
<li><a href="categories/">Manage Joke Categories</a></li>
</ul>
</body>
</html>

index.php

<?php

//Display author list
include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
try {
$result = $pdo->query('SELECT id, name FROM author');
} catch (PDOException $e){
$error = 'Error fetching authors from database! ' . $e->getMessage();
include 'error.html.php';
exit();
}

if (isset($_POST['action']) and $_POST['action'] == 'Delete')
{
include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
//Get jokes belonging to author
try {
$sql = 'SELECT id FROM joke WHERE authorid = :id';
$s = $pdo->prepare($sql);
$s->bindValue(':id', $_POST['id']);
$s->execute();
} catch (PDOException $e){
$error = 'Error fetching authors with their jokes! ' . $e->getMessage();
include 'error.html.php';
exit();
}

$result = $s->fetchAll();

//Delete joke category entries
try {
$sql = 'DELETE FROM jokecategory WHERE jokeid = :id';
$s = $pdo->prepare($sql);

//For each joke
foreach ($result as $row)
{
$jokeId = $row['id'];
$s->bindValue(':id', $jokeId);
$s->execute();
}
} catch (PDOException $e){
$error = 'Error deleting joke category! ' . $e->getMessage();
include 'error.html.php';
exit();
}

//Delete jokes belonging to author
try {
$sql = 'DELETE FROM joke WHERE authorid = :id';
$s = $pdo->prepare($sql);
$s->bindValue(':id', $_POST['id']);
$s->execute();
} catch (PDOException $e){
$error = 'Error deleting joke from a specific author! ' . $e->getMessage();
include 'error.html.php';
exit();
}

//Delete the author
try {
$sql = 'DELETE FROM author WHERE id = :id';
$s = $pdo->prepare($sql);
$s->bindValue(':id', $_POST['id']);
$s->execute();
} catch (PDOException $e){
$error = 'Error deleting the author from database! ' . $e->getMessage();
include 'error.html.php';
exit();
}

header('Location: .');
exit();
}

foreach ($result as $row){
$authors[] = array(
'id' => $row['id'],
'name' => $row['name']
);
}

include 'authors.html.php';
?>

authors.html.php

<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/helper.inc.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>Manage Authors</title>
</head>
<body>
<h1>Manage Authors</h1>
<p><a href="?add">Add new author</a></p>
<ul>
<?php foreach($authors as $author): ?>
<li>
<form action="" method="post">
<div>
<?php echo htmlout($author['name']); ?>
<input type="hidden" value="<?php echo htmlout($author['id']); ?>"/>
<input type="submit" name="action" value="Edit"/>
<input type="submit" name="action" value="Delete"/>
</div>
</form>
</li>
<?php endforeach; ?>
</ul>
<p><a href="..">Return to JMS home</a></p>
</body>
</html>

所有的投入都受到高度赞赏。

最佳答案

  • 我不会询问 $pdo 对象是否已正确初始化以及数据库中是否有实际记录。您可以使用 Workbench 等外部 MySQL 客户端来测试查询。您也用它来优化。只有当您对查询感到满意时,才将其放入脚本中,除非它非常简单,您可以就地正确编写。
  • `table_names``column_names` 转义为 MySQL 合法版本,并使用 ; 结束查询。而且看起来更好。
  • 此外,在 foreach 之前初始化变量 $authors = array();。那本书应该教你这一点。
  • 学习使用var_dump()。将 var_dump($result); 放在 $result = $pdo->query('...'); 之后。还可以在 foreach 中使用 var_dump($row);。它用于简单但有效的调试。打印变量值。
  • htmlout() 确实存在吗?
  • 添加 var_dump() 后您实际上看到了什么?

关于php - fetchAll() 没有结果;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22141807/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com