mysql_fetch_assoc错误,似乎无法弄清问题是什么

时间:2010-03-03 14:27:26

标签: php mysql

  

可能重复:
  “Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given” error while trying to create a php shopping cart

我在这里一直收到这个错误:

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\ReadNotes\viewyournote.php on line 40

使用此代码:

<?php

//Starting session

session_start();

//Includes mass includes containing all the files needed to execute the full script
//Also shows homepage elements without customs

include ('includes/mass.php');

//Set the  session variable

$username = $_SESSION['username'];

//Get variables from form

$chapter = substr($_GET['chapter'],1,-1);

$id      = substr($_GET['note'],1,-1);

$user    = substr($_GET['user'],1,-1);

$author  = substr($_GET['author'],1,-1);

$book    = substr($_GET['book'],1,-1);

//Check if isset(username)///

        if (isset($username))

            //Execute view

            {

                $sql_to_view_the_note = "SELECT * FROM notes INNER JOIN small_note ON notes_id = '$id' AND authname = '$author' AND bookname = '$book' AND user = '$username'";

                $sql_view_query = mysql_query($sql_to_view_the_note);

                while ($view_row = mysql_fetch_assoc($sql_view_query))

                      {
                        //Define values to view to user

                        $author_v = $view_row['authname'];
                        $bookname_v = $view_row['bookname'];
                        $chapter_name_v = $view_row['chapter_name'];
                        $smallnote_v  = $view_row['small_note'];    

                      }     

                                    //Echo back author name and book name

                                    echo "<center>";

                                    echo "<br><br>";

                                    echo "<div id= 'authorname'>";

                                    echo "Author's Name: ";

                                    echo $author_v;

                                    echo "</div>";

                                    echo "</div>";

                                    //Echo back book name

                                    echo "<br>";

                                    echo "<div id= 'book_name'>";

                                    echo "Books Name: ";

                                    echo $bookname_v;

                                    echo "</div>";

                                    echo "</center>";

                                    //Echo back chapter

                                    echo "<br>";

                                    echo "<div id= 'book_name'>";

                                    echo "Chapter Name: ";

                                    echo $chapter_name_v;

                                    echo "</div>";

                                    echo "</center>";

                                    //Echo back note

                                    echo "<br>";

                                    echo "<div id= 'book_name'>";

                                    echo "Small notes: ";

                                    echo $smallnote_v;

                                    echo "</div>";

                                    echo "</center>";
            }





?>

4 个答案:

答案 0 :(得分:2)

在使用mysql_query

之前,您应该始终检查mysql_fetch_assoc的返回值

当执行查询失败时mysql_query返回false(布尔值),当您将此作为mysql_fetch_assoc的输入时,您会收到错误。

在mysql_query之前打印查询,在mysql中运行它,看看它是否正常工作。

答案 1 :(得分:0)

如果发生错误,

mysql_query会返回false。致电mysql_error以了解原因。

顺便说一句,如果你可以转移到mysqli

答案 2 :(得分:0)

PHP docs开始,mysql_query在成功时返回资源(查询结果),并在出错时返回布尔值FALSE。看起来你对mysql_query()的调用可能返回false,表明你的SQL出错了。

答案 3 :(得分:0)

即使认为这不是问题的一部分,出于安全原因,您也不应该直接在DB中插入$ _GET中的内容。你需要先消毒它。看看mysql_real_escape_string