Reading information from database

Discussion in 'PHP' started by j_o, Mar 31, 2011.

  1. #1
    Hi everyone,

    I have information posted on a database that I am trying to get my script to read based on one of the elements in the table matching with the supplied name (ie. the title)

    This is the data that I have in my first table.
    [​IMG]

    This is a segment of my code which is requesting the information from this table
      <?php
        $con = mysql_connect("localhost"," ***** "," ***** ");
    	mysql_select_db("jmazza17_photos", $con);
    		
    	$album_title =('Test album');
    	$album_description = mysql_query("SELECT album_description FROM album_info WHERE album_title='{$album_title}'",$con); 
    	$total_photos = mysql_query("SELECT photos FROM album_info WHERE album_title='{$album_title}'",$con); 
    		
    	if (!$con){
      		die('Could not connect: ' . mysql_error());
      	}
    	
    	if (!mysql_query($sql,$con)){
    	  die('Error: ' . mysql_error());
      	}
    	?>
    PHP:
    When I load the page I get this error:
    But as you can see from the image above the table is not empty.


    Any ideas how I can fix this. Thanks in advance.
     
    j_o, Mar 31, 2011 IP
  2. AlC4Tr4z

    AlC4Tr4z Member

    Messages:
    29
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #2
    Try with backtick:

    $album_description = mysql_query("SELECT `album_description` FROM `album_info` WHERE `album_title`='{$album_title}'",$con);
    $total_photos = mysql_query("SELECT `photos` FROM `album_info` WHERE `album_title`='{$album_title}'",$con);


    cheers
     
    AlC4Tr4z, Mar 31, 2011 IP
  3. j_o

    j_o Well-Known Member

    Messages:
    516
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    113
    #3
    Hi AlC4Tr4z thanks for the quick reply. Unfortunately that did not work. Do you have any other ideas?
     
    j_o, Mar 31, 2011 IP
  4. AlC4Tr4z

    AlC4Tr4z Member

    Messages:
    29
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #4
    Hi,
    at this moment any sorry, I'm going to work now, if u can provide the db dump later in the evening I'll try some more solution.

    Cheers
     
    AlC4Tr4z, Mar 31, 2011 IP
    j_o likes this.
  5. SametAras

    SametAras Well-Known Member

    Messages:
    53
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    113
    #5
    Where is the $sql variable? I can not see it.

    You must to do so:

    
    <?php
        $con = mysql_connect("localhost"," ***** "," ***** ");
        mysql_select_db("jmazza17_photos", $con);
        if (!$con){
            die('Could not connect: ' . mysql_error());
        }
        
        $album_title = 'Test album';
        $album_inf = mysql_query("SELECT album_description,photos FROM album_info WHERE album_title='{$album_title}'",$con);
    
        if (!$album_inf){
          die('Error: ' . mysql_error());
        }
        $row = mysql_fetch_row($album_inf);
        echo $row[0];
        echo $row[1];
    ?>
    
    
    
    PHP:
     
    SametAras, Apr 1, 2011 IP
  6. codeartist

    codeartist Peon

    Messages:
    96
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Please debug the script as follows.

    $album_title = 'Test album';
    $sql="SELECT album_description,photos FROM album_info WHERE album_title='{$album_title}'";
    echo $sql;
    exit;
    $album_inf = mysql_query($sql,$con);

    and see if the correct sql command is printing
     
    codeartist, Apr 3, 2011 IP
    j_o likes this.
  7. j_o

    j_o Well-Known Member

    Messages:
    516
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    113
    #7
    Thanks for the help, that part of the script is now working properly and it is finding the album information.

    I am now trying to make it retrieve file names and file descriptions for each picture and I am running into some problems doing the same thing. This is the code that tries to retrieve that information.(its in the same file as the previous problem so the connection is still active).

    
    <table  border="0" cellspacing="0" cellpadding="0" align="center">    
         <?php
    	  $count = 1;
    	  $pic_num = 1;
    	  $remainder = $total_photos % 6;
    	  $full_row = ($total_photos - $remainder)/6;
    	  
    	  while($count <= $full_row){
         ?>
    		<tr>
          <?php
    	  	$j = 1;
    		while ($j  <= 6){
    			$file_info = mysql_query("SELECT pic_name,pic_description FROM pics WHERE album_title='{$album_title}' AND count = '{$pic_num}'",$con);
    			if (!$file_info){
    			  die('Error: ' . mysql_error());
    			}
    			$row = mysql_fetch_row($file_info);
    			$file_name = $row[0];
    			$file_description = $row[1];
    	  ?> 
                <td><a href="<?php echo($album_title); ?>/photos/<?php echo($file_name); ?>" rel="lytebox[vacation]" title="<b><?php echo($file_description); ?></b>">
                <img src="<?php echo($album_title); ?>/thumbs/<?php echo($file_name); ?>" width="160" height="120" border="0" alt="Name:<?php echo($file_name);?>"></a></td> 
                
          <?php
    			$j = $j + 1;
    			$pic_num = $pic_num + 1;
    		}
    	  ?>
    		</tr>
    	<?php
    		$count = $count + 1;
    	  }
    	?>
        </table>
    
    Code (markup):

    Any help is once again appreciated, thanks for the help.
     
    j_o, Apr 3, 2011 IP
  8. j_o

    j_o Well-Known Member

    Messages:
    516
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    113
    #8
    I have actually managed to figure the problem out myself. Thanks again though for the initial help.
     
    j_o, Apr 3, 2011 IP