2015-02-24 75 views
1

我想從MySql表中檢索數據。我使用的XAMPP,phpMyAdmin的等...我也跟着一步本教程一步:https://www.youtube.com/watch?v=IHdd02IK2Jg未定義的變量:第29行的C: XAMPP htdocs display_prsc.php中的記錄

但我得到這個錯誤:

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> 
+2

如果在教程的傢伙使用'mysql_ *'我會強烈建議你停止與此教程!並閱讀此:http://php.net/manual/en/book.pdo.php – Rizier123 2015-02-24 09:36:56

+0

這段代碼中有很多錯誤,非常可怕的寫。 – Raptor 2015-02-24 09:47:08

+0

你的html是一個完整的混亂! – Rizier123 2015-02-24 10:19:19

回答

3

請注意您的第一個php標籤中的空間。 你有<? php,它應該是<?php

而且,因爲看起來你開始學習,所以請避免學習廢棄的東西。不要使用MySQL,使用PDO或Mysqli。

+0

是的,好,趕上! +1 – student 2015-02-24 09:44:02

+1

這不是OP代碼中唯一的錯誤! – Rizier123 2015-02-24 09:46:46

+0

嗨,看不到他正在追蹤的視頻,我只是盡我所能幫助他:) – lmarcelocc 2015-02-24 09:48:59

1

嘗試

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

你在你的代碼的一些錯誤:

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_*與預處理語句,或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); 
?> 

錯誤報告應該只在分期中完成,而不是生產。

0

你的代碼有多個錯誤。但對於錯誤,它是由於mysql_query()返回FALSE在行:

$records=mysql_query($sql); 

於是,線路while ($new=mysql_fetch_assoc($records)){不能得到$records價值,從而產生錯誤。


其他錯誤是:

HTML

  • 缺少DOCTYPE
  • 失蹤元字符集
  • 缺少</tr>
  • 放錯了地方</head>
  • 是現代的,使用CSS樣式,而不是使用HTML標記您的HTML屬性

PHP

  • 使用它們的過時mysql_*功能
  • 使用前沒有檢查函數的返回值;使用PDO /庫MySQLi而不是
  • 額外的空間在<? php
+1

*缺少 *否:D看OP的HTML的結尾。很不好的HTML。也看看OP的第一排表... – Rizier123 2015-02-24 10:21:00

+0

好趕上!一個錯位的頭 – Raptor 2015-02-24 10:29:38

相關問題