未定义变量:第29行C:\XAMPP\htdocs\display_prsc.php中的记录

Undefined variable: records in C:\XAMPP\htdocs\display_prsc.php on line 29

我想从 MySql table 检索数据。我正在使用 Xampp、phpMyAdmin 等...我一步一步地按照本教程进行操作:https://www.youtube.com/watch?v=IHdd02IK2Jg

但是我得到这个错误:

Notice: Undefined variable: records in C:\XAMPP\htdocs\display_prsc.php on line 29

还有一个警告:

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, null given in C:\XAMPP\htdocs\display_prsc.php on line 29

我 运行 它只显示列名。

    <? php

       //make connection
       mysql_connect('localhost','root','');

       //select_db
       mysql_select_db('mynewdb');

       $sql="SELECT * FROM new";

       $records=mysql_query($sql);
    ?>

    <html>
    <head>
    <title>Program Scores Table</title>

    <body>

     <table width="600" border="1" cellpadding="1" cellspacing="1">
     <tr>
             <th>Year</th>
             <th>Criteria</th>
             <th>Score</th>
     <tr>

    <?php

       while ($new=mysql_fetch_assoc($records)){

             echo"<tr>";
             echo"<td>".$new['py']."</td>";
             echo"<td>".$new['pcrit']."</td>";
             echo"<td>".$new['psco']."</td>";
             echo"</tr>";
       }
     ?>

      </table>
      </body>
      </head>
      </html>

注意第一个 php 标签中的 space。 你有 <? php ,应该是 <?php

并且由于您似乎开始学习,因此请避免学习已弃用的内容。不要使用MySQL,使用PDO或Mysqli。

我建议您使用 MySQL 的 Prepared 语句而不是干净的 MySQL,它一点也不安全,以后会给您带来问题。

这里是里尔指南。
http://www.w3schools.com/pHp/php_mysql_prepared_statements.asp

尝试

<?php

       //make connection
      $con= mysql_connect('localhost','root','');

       //select_db
      $select= mysql_select_db('mynewdb',$con);

       $sql="SELECT * FROM new";

       $records=mysql_query($sql);
    ?>

    <html>
    <head>
    <title>Program Scores Table</title>

    <body>

     <table width="600" border="1" cellpadding="1" cellspacing="1">
     <tr>
             <th>Year</th>
             <th>Criteria</th>
             <th>Score</th>
     <tr>

    <?php

       while ($new=mysql_fetch_array($records)){

             echo"<tr>";
             echo"<td>".$new['py']."</td>";
             echo"<td>".$new['pcrit']."</td>";
             echo"<td>".$new['psco']."</td>";
             echo"</tr>";
       }
     ?>

      </table>
      </body>
      </head>
      </html>

您的代码中有一些错误:

1。错误的 php 开头标签

您必须删除 php 起始标签中的 space,例如

<?php

2。奇怪html

你几乎整个html都错了!我建议您阅读一本书或观看基本 html 教程

因此您的整个代码应如下所示:

<?php

    //make connection
    mysql_connect('localhost', 'root', '');

    //select_db
    mysql_select_db('mynewdb');

    $sql = "SELECT * FROM new";

    $records = mysql_query($sql);

?>

<html>
    <head>
        <title>Program Scores Table</title>
    </head> <!-- head tag closed here -->

    <body>

        <table width="600" border="1" cellpadding="1" cellspacing="1">
            <tr>
                <th>Year</th>
                <th>Criteria</th>
                <th>Score</th>
            </tr> <!-- forgot / to close the tag -->

            <?php
            while ($new = mysql_fetch_assoc($records)){
                echo "<tr>";
                echo "<td>" . $new['py'] . "</td>";
                echo "<td>" . $new['pcrit'] . "</td>";
                echo "<td>" . $new['psco'] . "</td>";
                echo "</tr>";
            }
            ?>

      </table>

    </body>   
</html>

旁注:

1。有什么可以改进的?

  • 我会做一些基本的错误检查,例如如果连接失败等等
  • mysqli_* with prepared statements, or PDO与准备好的语句一起使用,它们更安全! (我们生活在 2015 年)
  • (另外,如果您教程中的人使用 mysql_*,请更改教程!)

这样您不仅可以使用旧代码,还可以使用改进后的代码:

<?php

        //Database stuff
        $hostname = "localhost";
        $username = "root";
        $password = "";
        $dbname = "mynewdb";

        try {

            $dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
            $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


            $stmt = $dbh->prepare("SELECT * FROM new");
            $stmt->execute();


            //Close Connection
            $dbh = null;

        } catch(PDOException $e) {
            echo $e->getMessage();
        }

?>

<html>
    <head>
        <title>Program Scores Table</title>
    </head>

    <body>

        <table width="600" border="1" cellpadding="1" cellspacing="1">
            <tr>
                <th>Year</th>
                <th>Criteria</th>
                <th>Score</th>
            </tr> 

            <?php
            while ($new = $stmt->fetch(PDO::FETCH_ASSOC)){
                echo "<tr>";
                echo "<td>" . $new['py'] . "</td>";
                echo "<td>" . $new['pcrit'] . "</td>";
                echo "<td>" . $new['psco'] . "</td>";
                echo "</tr>";
            }
            ?>

      </table>

    </body>   
</html>

2。编码旁注

error reporting 添加到您的文件的顶部,这将有助于查找错误。

<?php
    ini_set("display_errors", 1);
    error_reporting(E_ALL);
?>

错误报告只能在试运行中进行,绝不能在生产中进行。

您的代码中存在多个错误。但对于错误,这是由于 mysql_query() returns FALSE at line:

$records=mysql_query($sql);

然后,while ($new=mysql_fetch_assoc($records)){行无法获取$records的值,从而产生错误。


其他错误是:

HTML

  • 缺少 DOCTYPE
  • 缺少元字符集
  • 少了一个</tr>
  • 一个错位</head>
  • 现代,使用 CSS 来设计你的 HTML 而不是使用 HTML 标签属性

PHP

  • 在使用函数之前没有检查 return 函数值
  • 使用已弃用的 mysql_* 函数;改为使用 PDO / MySQLi
  • 额外 space 在 <? php