tags:

views:

69

answers:

4

I keep getting this error here:

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

With this code here:

<?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>";
            }





?>
+2  A: 

You should always check the return value of mysql_query before you use mysql_fetch_assoc

When the execution of query fails mysql_query returns false ( a boolean) and when you give this as an input to mysql_fetch_assoc, you get your error.

Print the query just before mysql_query, run it in mysql and see if it works correctly.

codaddict
And since it says a boolean is given, it's likely `false` indicating a MySQL error. `if(!$query) { die(mysql_error()); }`
Matchu
A: 

mysql_query return false if an error occured. call mysql_error to know why.

by the way, if you can move to mysqli

mathroc
A: 

From the PHP docs, mysql_query returns a resource on success (the query results), and a boolean FALSE on error. It looks like your call to mysql_query() is probably returning false, indicating an error with your SQL.

Brian
A: 

Even thought it's not part of the question, for security reasons you shouldn't insert stuff from $_GET directly in the DB. You need to sanitize it first. Take a look at mysql_real_escape_string

easement