【问题标题】:Undefined variable: records in C:\XAMPP\htdocs\display_prsc.php on line 29未定义变量:第 29 行 C:\XAMPP\htdocs\display_prsc.php 中的记录
【发布时间】:2015-04-25 20:47:07
【问题描述】:

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

但我收到此错误:

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

还有一个警告:

警告:mysql_fetch_assoc() 期望参数 1 是资源,在第 29 行的 C:\XAMPP\htdocs\display_prsc.php 中给出 null

当我运行它时它只显示列名。

    <? 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>

【问题讨论】:

  • 如果在你的教程中这个人使用mysql_*,我强烈建议你停止本教程!并阅读:php.net/manual/en/book.pdo.php
  • 这段小代码有很多错误,写的很烂。
  • 你的html是一团糟!

标签: php html mysql apache html-table


【解决方案1】:

您的代码中有一些错误:

1。错误的php开始标签

您必须删除 php 开始标记中的空格,例如

<?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_* 与prepared statements 一起使用,或者PDO 与prepared statements 一起使用,它们更安全! (我们生活在 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);
?>

错误报告应该只在暂存阶段完成,而不是在生产阶段。

【讨论】:

    【解决方案2】:

    注意第一个 php 标签中的空格。 你有 &lt;? php ,应该是 &lt;?php

    既然您似乎开始学习,请避免学习已弃用的内容。不要使用 MySQL,使用 PDO 或 Mysqli。

    【讨论】:

    • 您好,看不到他正在关注的视频,我只是尽力帮助他:)
    • OP,如果您切换到 IDE(例如 NetBeans),您会立即发现此标记错误,因为它会更改代码颜色。
    【解决方案3】:

    试试

    <?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>
    

    【讨论】:

      【解决方案4】:

      我建议你对 MySQL 使用 Prepared statements 而不是 clean MySQL,它根本不安全,以后会给你带来问题。

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

      【讨论】:

        【解决方案5】:

        您的代码中有多个错误。但是对于错误,这是由于mysql_query() 在以下行返回 FALSE

        $records=mysql_query($sql);
        

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


        其他错误是:

        HTML

        • 缺少 DOCTYPE
        • 缺少元字符集
        • 缺少&lt;/tr&gt;
        • 一个错位的&lt;/head&gt;
        • 现代一点,使用 CSS 来设置 HTML 样式,而不是使用 HTML 标签属性

        PHP

        • 在使用函数之前没有检查函数的返回值
        • 使用已弃用的mysql_* 函数;改用 PDO / MySQLi
        • &lt;? php 的额外空间

        【讨论】:

        • missing no :D 查看 OP 的 html 结尾。很糟糕的html。另请查看 OP 中的第一行表...
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-05-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-05
        相关资源
        最近更新 更多