mysql_fetch_assoc() expects parameter 1 to be resource, boolean

Discussion in 'PHP' started by p4d4y, Feb 2, 2012.

  1. #1
    Hello. I've been working on a PHP project but I have this error:

    mysql_fetch_assoc() expects parameter 1 to be resource, boolean given inC:\xampp\htdocs\Soundflex\func\album.func.php on line 22


    Here's my album.func.php code

    <?phpfunction album_data($album_id) {
    }
    function album_check($album_id) {
    }
    function get_albums() {  $albums = array();    $albums_query = mysql_query("  SELECT `albums`.`album_id`, `albums`.`timestamp`, `albums`.`name`, LEFT(`albums`.`description`, 50) as `description`, COUNT(`songs`.`song_id`) as `song_count`  FROM `albums`  LEFT JOIN `songs`  ON `albums`.`album_id` = `songs`.`album_id'  WHERE `albums`.`user_id` = ".$_SESSION['user_id']."  GROUP BY `albums`.`album_id'  ");
      while ($albums_row = mysql_fetch_assoc($albums_query)) {    $albums[] = array(              'id' => $albums_row['album_id'],              'timestamp' => $albums_row['timestamp'],              'name' => $albums_row['name'],              'description' => $albums_row['description'],              'count' => $albums_row['song_count']    );  }
      return $albums;}
    function create_album($album_name, $album_description) {  $album_name = mysql_real_escape_string(htmlentities($album_name));  $album_description = mysql_real_escape_string(htmlentities($album_description));
      mysql_query("INSERT INTO `albums` VALUES ('', '".$_SESSION['user_id']."', UNIX_TIMESTAMP(), '$album_name', '$album_description')");  mkdir('uploads/'.mysql_insert_id(), 0744);  mkdir('uploads/thumbs/'.mysql_insert_id(), 0744);}
    function edit_album($album_id, $album_name, $album_description) {  }
    function delete_album($album_id) {
    }
    ?>
    PHP:
    And here's my albums.php code just in case

    <?phpinclude 'init.php';
    if(!logged_in()) {  header('Location: index.php');  exit();}
    include 'template/header.php';?>
    <h3>Albums</h3>
    <?php$albums = get_albums();
    if (empty($albums)) {  echo "<p>You haven't created any albums yet!</p>";} else {  foreach ($albums as $album) {    echo '<p><a href="view_album.php?album_id=', $album['id']. '">', $album['name'], '</a> (', $album['count'], ' images) <br />    ', $album['description'], '...<br/>    <a href="edit_album.php?album_id=', $album['id'], '">Edit</a> / <a href="delete_album.php?album_id=', $album['id'], '">Delete</a>    </p>';  }}
    include 'template/footer.php';?>
    PHP:
    Thanks.
     
    p4d4y, Feb 2, 2012 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Your query is most likely failing.

    Replace:

    $albums_query = mysql_query(" SELECT `albums`.`album_id`, `albums`.`timestamp`, `albums`.`name`, LEFT(`albums`.`description`, 50) as `description`, COUNT(`songs`.`song_id`) as `song_count` FROM `albums` LEFT JOIN `songs` ON `albums`.`album_id` = `songs`.`album_id' WHERE `albums`.`user_id` = ".$_SESSION['user_id']." GROUP BY `albums`.`album_id' ");

    with:

    if(!$albums_query = mysql_query(" SELECT `albums`.`album_id`, `albums`.`timestamp`, `albums`.`name`, LEFT(`albums`.`description`, 50) as `description`, COUNT(`songs`.`song_id`) as `song_count` FROM `albums` LEFT JOIN `songs` ON `albums`.`album_id` = `songs`.`album_id' WHERE `albums`.`user_id` = ".$_SESSION['user_id']." GROUP BY `albums`.`album_id' ")) {
    die(mysql_error());
    }
     
    jestep, Feb 3, 2012 IP
  3. NothingLikeThis

    NothingLikeThis Member

    Messages:
    113
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #3
    is this works with mysql_fetch_array() as i am facing the same issue with my specified function?
    '
     
    NothingLikeThis, Feb 4, 2012 IP
  4. Andre91

    Andre91 Peon

    Messages:
    197
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    0
    #4
    use mysql_fetch_array
     
    Andre91, Feb 4, 2012 IP
  5. NothingLikeThis

    NothingLikeThis Member

    Messages:
    113
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #5
    i m facing the problem with mysql_fetch_array itself
     
    NothingLikeThis, Feb 4, 2012 IP
  6. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #6
    You need to figure out what error the query is throwing. mysql_fetch_array and ..._assoc are essentially the same function. Your query is returning an error or nothing at all so you cannot fetch an array from it.
     
    jestep, Feb 6, 2012 IP