简体   繁体   English

当尝试使用INNER JOIN关联两个表时,我的php模板中的foreach()错误

[英]foreach() error in my php template when trying to relate two tables using INNER JOIN

I´m reading Kevin Yank´s book " PHP and MySQL Novice to Ninja 5th edition ", and found an error there in the code, and would like someone to help me out with it, maybe is a silly typo...? 我在阅读Kevin Yank的书“ Ninja 5版本的PHP和MySQL新手 ”,发现代码中有错误,希望有人帮我解决这个问题,也许是一个愚蠢的错字...?

I´m trying to follow the author´s example of creating and accessing a database of jokes. 我正在尝试遵循作者创建和访问笑话数据库的示例。 I´m learning how to join two databases to show with php a list of all the jokes. 我正在学习如何加入两个数据库以用php显示所有笑话的列表。 I have two databases joke and author. 我有两个数据库笑话和作者。 I´ve got this: 我有这个:

try{ 
    $sql = 'SELECT joke.id, joketext, jokedate, name, email
    FROM joke INNER JOIN author
    ON authorid = author.id';
    $result = $pdo->query($sql);
}
catch (PDOException $e)
{
    $error = 'Error: ' . $e->getMessage();
    include 'error.html.php';
    exit();
}

foreach ($result as $row)
{
    $jokes[] = array(
    'id' => $row['id'], 
    'text' => $row['joketext'], 
    'date' => $row['jokedate'],
    'name' => $row['name'],
    'email' => $row['email']
    );
}
include 'jokes.html.php';

Now, all was working ok, until I´ve replaced the simple code to select the database information from just one table, to the INNER JOIN code. 现在,一切正常,直到我替换了简单的代码以从一个表中选择数据库信息,再替换为INNER JOIN代码。 This is the book´s code, wich I´ve followed. 这是我一直遵循的代码。

In the jokes.html.php file, I´ve got this (wich I think is what´s giving me the error): 在jokes.html.php文件中,我得到了这个(我认为这是给我错误的原因):

    foreach($jokes as $joke): 
      <form action="?deletejoke" method="post">
  <?php 
      echo 'id. ';
  echo htmlspecialchars($joke['id'], ENT_QUOTES, 'UTF-8');
  echo htmlspecialchars($joke['date'], ENT_QUOTES, 'UTF-8');
  echo htmlspecialchars($joke['text'], ENT_QUOTES, 'UTF-8'); 
  echo htmlspecialchars($joke['name'], ENT_QUOTES, 'UTF-8');
      echo htmlspecialchars($joke['email'], ENT_QUOTES, 'UTF-8');
  ?>
  <input type="hidden" name="id" value="<?php echo $joke['id'];?>">
  <input type="submit" value="Borrar">
  ?>
  <br></form>
<?php endforeach; ?>

Now, the error that throws me is: 现在,引发我的错误是:

Notice: Undefined variable: jokes in C:\\xampp\\htdocs\\workspace1\\jokes.html.php on line 10 注意:未定义变量:在第10行的C:\\ xampp \\ htdocs \\ workspace1 \\ jokes.html.php中开玩笑

Warning: Invalid argument supplied for foreach() in C:\\xampp\\htdocs\\workspace1\\jokes.html.php on line 10 警告:在第10行的C:\\ xampp \\ htdocs \\ workspace1 \\ jokes.html.php中为foreach()提供了无效的参数

Line 10 of jokes.html.php is: jokes.html.php的第10行是:

foreach($jokes as $joke):

I´m trying to get more information about foreach() but I can´t spot the error... If anyone could help me out a bit (or maybe a clue!) I would be very grateful. 我正在尝试获取有关foreach()的更多信息,但我无法发现错误……如果有人可以帮助我(或提供线索!),我将不胜感激。 Thanks!!! 谢谢!!! Rosamunda 罗萨蒙达

UPDATE: 更新:

As the result of the query (trying it directely from phpmyadmin) was zero, so there were no database results for that query. 由于查询的结果(直接从phpmyadmin尝试)为零,因此该查询没有数据库结果。 I´ve decided to manually add one result doing this: 我决定手动添加一个执行此操作的结果:

INSERT INTO joke SET
joketext = 'this is a new joke....',
jokedate = '2012-01-01',
authorid = 1;

Now, the errors have dissapear, and that single results does show. 现在,错误消失了,而且确实显示了单个结果。

What I don´t understand is: 我不明白的是:

Why didn´t just no result showed up, instead of those errors? 为什么不显示结果而不显示错误?

How do you manage these situations? 您如何处理这些情况? I mean, it can happen that a query just have no results at all, is it common to result in those errors? 我的意思是,查询可能根本没有结果,这会导致这些错误吗?

One of your helpful comments says that $result is empty... so why when the query isn´t zero those errors won´t show up? 您的一条有用的评论说$ result为空...那么为什么当查询不为零时这些错误不会出现?

Thanks again for your help!!! 再次感谢你的帮助!!! Rosamunda 罗萨蒙达

I´ve found the answer in a SitePoint forum (the Book´s forum) , and I thought that it would be nice to post the question here, just in case anyone wonders, or just in case anyone out there happens to have the same problem. 我已经在SitePoint论坛(Book's论坛)中找到了答案,我认为最好在这里发布问题,以防万一有人想知道,或者万一有人碰巧有相同的问题问题。

It is because of your php settings to show Notices and Warnings. 这是因为您的php设置显示通知和警告。 The notice/warning is valid because you were attempting to use the $jokes variable before you declared/assigned a value to it. 通知/警告有效,因为您在声明/分配值之前尝试使用$ jokes变量。 You can solve this by putting $jokes = array(); 您可以通过将$jokes = array();来解决此问题$jokes = array(); before PHP Code: 在PHP代码之前:

 foreach ($result as $row) { $jokes[] = array( 'id' => $row['id'], 'text' => $row['joketext'], 'date' => $row['jokedate'], 'name' => $row['name'], 'email' => $row['email'] ); } 

That will at least declare the $jokes variable for in the event that there are zero results. 如果结果为零,则至少将声明$ jokes变量。

So, I think the conclusion (please correct me if I´m wrong here!) is that you should always declare any variable that you pretend to use, just in case it happens to have no results. 因此,我认为结论(如果我在这里错了,请纠正我!)是, 您应该始终声明任何要假装使用的变量,以防万一碰巧没有结果。 Because if it is empty it will show a nasty error message that will freak you out. 因为如果为空,则会显示令人讨厌的错误消息,使您感到恐惧。

And to declare a variable you use $variablename = array() . 要声明一个变量,可以使用$variablename = array()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM