SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Thread: query problems?

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    query problems?

    I'm trying to get the query to show all of the rows, but all its doing is just duplicating the first row of data.

    PHP Code:
    if (file_exists($dir) == false
    {
    echo 
    'Folder \''$dir'\' not found!';
    } else {
    $dir_contents scandir($dir);

    foreach (
    $dir_contents as $file
    {
        
    $file_type strtolower(end(explode('.'$file)));
        
        if (
    $file !== '.' && $file !== '..' && in_array($file_type$file_display) == true
        {
            
    $animeimg '<img src="' $dir '/' $file '" alt="' $file '" width="80px;" height="100px" />';
            
            
    $sql "SELECT image, name, description FROM phpbb_anime";
            
    $result $db->sql_query($sql);
            
    $row $db->sql_fetchrow($result);
            while (
    $numrow mysql_fetch_array($result)) {
                
    $animename $row['name'];
                
    $animedesc $row['description'];
                
    $animeimg $row['image'];
                }
            
            
    $template->assign_block_vars('anime', array(
                
    'ANIME_NAME'           => $row['name'],
                
    'ANIME_DESC'         => $row['description'],
                
    'ANIME_IMG'            => $animeimg,
                
               ));
        }
    }


  2. #2
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,840
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Try this:

    http://php.net/manual/en/function.mysql-fetch-array.php

    PHP Code:


    <?php
    mysql_connect
    ("localhost""mysql_user""mysql_password") or
        die(
    "Could not connect: " mysql_error());
    mysql_select_db("mydb");

    $result mysql_query("SELECT id, name FROM mytable");

    while (
    $row mysql_fetch_array($resultMYSQL_NUM)) {
        
    printf("ID: %s  Name: %s"$row[0], $row[1]);  
    }

    mysql_free_result($result);
    ?
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Patche on earning Member of the Month for July 2014

  3. #3
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this...

    while ($numrow = mysql_fetch_array($result)) {
    $animename = $numrow['name'];
    $animedesc = $numrow['description'];
    $animeimg = $numrow['image'];
    }

  4. #4
    SitePoint Member
    Join Date
    Apr 2012
    Location
    Los Angeles, CA
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or you can try this:

    Code PHP:
    <?php
    if (file_exists($dir) == false) 
    {
    	echo 'Folder \'', $dir, '\' not found!';
    } else {
    	$dir_contents = scandir($dir);
     
    	foreach ($dir_contents as $file) 
    	{
    		$file_type = strtolower(end(explode('.', $file)));
     
    		if ($file !== '.' && $file !== '..' && in_array($file_type, $file_display) == true) 
    		{
    			$animeimg = '<img src="' . $dir . '/' . $file . '" alt="' . $file . '" width="80px;" height="100px" />';
     
    			$sql = "SELECT image, name, description FROM phpbb_anime";
    			$result = $db->sql_query($sql);
     
    			while ( $row = mysql_fetch_assoc($result)) {
    				$animename 	= $row['name'];
    				$animedesc 	= $row['description'];
    				$animeimg 	= $row['image'];
    			}
     
    			$template->assign_block_vars('anime', array(
    				'ANIME_NAME'	=> $row['name'],
    				'ANIME_DESC'	=> $row['description'],
    				'ANIME_IMG'	=> $animeimg,
    			));
    		}
    	}
    }

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I probably should have mentioned that I'm getting the error "mysql_fetch_assoc() expects parameter 1 to be resource, object given"

  6. #6
    SitePoint Member
    Join Date
    Apr 2012
    Location
    Los Angeles, CA
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can i see the $db class you are using? mysql_fetch_assoc and mysql_fetch_array only work with the mysql_connect method. Unless the $db class uses that as the base connection method, you may need to switch to that instead of you db class if you want to use mysql_fetch_array or mysql_fetch_assoc.

  7. #7
    SitePoint Member
    Join Date
    Apr 2012
    Location
    Los Angeles, CA
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure if you need to use the db class but this should work:

    Code PHP:
    	$conn = mysql_connect( HOST, USERNAME, PASSWORD );
    	mysql_select_db( DBNAME );
     
        if (file_exists($dir) == false) 
        {
        	echo 'Folder \'', $dir, '\' not found!';
        } else {
        	$dir_contents = scandir($dir);
     
        	foreach ($dir_contents as $file) 
        	{
        		$file_type = strtolower(end(explode('.', $file)));
     
        		if ($file !== '.' && $file !== '..' && in_array($file_type, $file_display) == true) 
        		{
        			$animeimg = '<img src="' . $dir . '/' . $file . '" alt="' . $file . '" width="80px;" height="100px" />';
     
        			$sql = "SELECT image, name, description FROM phpbb_anime";
        			$result = mysql_query( $sql, $conn );
     
        			while ( $row = mysql_fetch_assoc($result)) {
        				$animename 	= $row['name'];
        				$animedesc 	= $row['description'];
        				$animeimg 	= $row['image'];
        			}
     
        			$template->assign_block_vars('anime', array(
        				'ANIME_NAME'	=> $row['name'],
        				'ANIME_DESC'	=> $row['description'],
        				'ANIME_IMG'	=> $animeimg,
        			));
        		}
        	}
        }

  8. #8
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The thing is, I'm not sure what $db is composed of, because I'm using phpbb, so I'm not sure from which file it originates from.

    Edit: I tried what devildoc34 said, and that didn't work either.

  9. #9
    SitePoint Member
    Join Date
    Apr 2012
    Location
    Los Angeles, CA
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure about how the db class works but from looking it up you can use the $db->sql_fetchrow() to get the row contents instead of using mysql_fetch_assoc.

  10. #10
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, the error went away, but so did all my data, and the images I had displayed just turn into file paths, with the same paths 3 times.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •