I need my php to call certain parts of a table from mysql

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • liosbeg
    New Member
    • Apr 2011
    • 5

    I need my php to call certain parts of a table from mysql

    I have a html code writtin which calls a php page.(this much works perfectly).

    In the html the user has a choice of 5 artists from a drop down menu.
    When the choose the artist I want it to call the details for that artist from mysql.

    In mysql:
    Table name in database:Tracks .
    Column 1 in table: album_number
    Column 2 in table: track number
    column 3 in table: track_title

    So if no 1 (james taylor) is called it should choose album no 1, all track numbers and all titles for album no 1.

    How do I do this.
    I have posted as far as i have gotten but I am stuck at this stage and going round in circles

    I am begging for your help.
    Thanks.

    Here is the php code.I have made so many changes I am lost now!

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> 
    <title>Album</title> 
    <link rel="stylesheet" type="text/css" href="homework.css"> 
    </head> 
    <body>  
    <?php  
    // get form selection  
    $day = $_GET['day']; 
          // check value and select appropriate item 
          if ($day == 1) { 
    $album = 'James Taylor'; 
    ?> 
    <?php 
          // set database server access variables: 
          $host = "testweb2.csis.ul.ie"; //localhost on your home PC/laptop
          $user = "XXXXXX"; //your ID number here
          $pass = "XXXXXX"; //your password here
          $db = "XXXXXX"; //your ID number here
    
          // open connection 
          $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); 
    
          // select database 
          mysql_select_db($db) or die ("Unable to select database!"); 
    
          // create query 
          $query = "SELECT * FROM Tracks"; 
    
          // execute query 
          $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 
    
          // see if any rows were returned 
          if (mysql_num_rows($result) > 0) { 
            // yes 
            // print them one after another 
            echo('<table class="query_result">'); 
            while($row = mysql_fetch_row($result)) { 
              echo('<tr>');
              for($c=0; $c<sizeof($row); $c++) {
                echo('<td>'.$row[$c].'</td>');
              } 
              echo('</tr>');
            } 
            echo('</table>');
          } 
          else { 
            // no 
            // print status message 
            echo('No rows found!');
          }
    
          // free result set memory 
          mysql_free_result($result); 
    
          // close connection 
          mysql_close($connection);
    
    
    
    
    
    }  
    elseif ($day == 2) { 
    $album = 'The Rolling Stones';  
    ?> 
    <table> 
    <tr> 
    <td> 
    <a href = index1.html>Back</a><br> 
    <h1>The Rolling Stones </h1> 
    <h2>'Symphany For The devil'</h2> 
    <img src="images/stones.jpg" alt="beggars banquet - rolling stones" width="100" height="100"/> 
    <p><span class="style1">Track Titles:<br /> 
    <h3> 
    <ol>1. Sympathy for the Devil</ol> 
    <ol>2. No Expectations</ol> 
    <ol>3. Parachute Woman</ol> 
    <ol>4. Jigsaw Puzzle</ol> 
    <ol>5. Street Fighting Man</ol>  
    <ol>6. Prodigal Son</ol> 
    <ol>7. Stray Cat Blues</ol>  
    <ol>8. Factory Girl</ol> 
    <ol>9. Salt of the Earth</ol> 
    </h3> 
    </span> 
    <br /> 
    <iframe title="YouTube video player" width="480" height="290" src="http://www.youtube.com/embed/iLddJ1WceHQ" frameborder="0" allowfullscreen></iframe> 
    </p> 
    </td> 
    </tr> 
    </table> 
    
    <?php  
    }  
    elseif ($day == 3) {  
     $album = 'Sting';  
    ?> 
    <table> 
    <tr> 
    <td> 
    <a href = index1.html>Back</a><br> 
    <h1>Sting</h1> 
    <h2>'All This Time'</h2> 
    <img src="images/allthistime.jpg" alt="All This Time - Sting" width="100" height="100"/> 
    <p><span class="style1">Track Titles:<br /> 
    <h3> 
    <ol>1. A Thousand Years</ol>
    <ol>2. If You Love Somebody Set Them Free</ol> 
    <ol>3. Perfect Love...Gone Wrong</ol>  
    <ol>4. All This Time</ol>  
    <ol>5. Seven Days</ol> 
    <ol>6. The Hounds of Winter</ol> 
    <ol>7. Dont Stand So Close to Me</ol>  
    <ol>8. When We Dance</ol>  
    <ol>9. Dienda</ol> 
    <ol>10. Roxanne</ol> 
    <ol>11. Every Breath You Take</ol> 
    </h3> 
    </span><br /> 
    </span><br /> 
    <iframe title="YouTube video player" width="480" height="290" src="http://www.youtube.com/embed/MsLzdYR_MaQ" frameborder="0" allowfullscreen></iframe> 
    </p> 
    </td> 
     </tr> 
    </table> 
    
    <?php  
     }  
    elseif ($day == 4) {  
    $album = 'Bob Dylan'; 
    ?> 
    <table> 
    <tr> 
    <td> 
    <a href = index1.html>Back</a><br> 
    <h1>'Bob Dylan'</h1> 
    <h2>'Hurricane'</h2> 
    <img src="images/dylan.jpg" alt="Bob Dylan Desire" width="100" height="100"/> 
    <p><span class="style1">Track Titles:<br /> 
    <h3> 
    <ol>1. Hurricane</ol>  
    <ol>2. Isis</ol> 
    <ol>3. Mozambique</ol> 
    <ol>4. One More Cup of Coffee (Valley Below)</ol> 
    <ol>5. Oh, Sister</ol> 
    <ol>6. Joey</ol> 
    <ol>7. Romance in Durango</ol> 
    <ol>8. Black Diamond Bay</ol> 
    <ol>9. Sara</ol> 
    </h3> 
    </span><br /> 
    <iframe title="YouTube video player" width="480" height="290" src="http://www.youtube.com/embed/OLDSdnHWaSU" frameborder="0" allowfullscreen></iframe> 
    </p> 
    </td> 
    </tr> 
    </table> 
    <?php  
    }  
    elseif ($day == 5) {  
    $album = 'Robert Plant and Jimmy Page';  
    ?> 
    	        <table> 
    	          <tr> 
    	        <td> 
    	        <a href = index1.html>Back</a><br> 
    	        <h1>Jimmy Page & Robert Plant </h1> 
    	        <h2>'Houses Of Holy'</h2> 
    	        <img src="images/housesofholy.jpg" alt="House of holy" width="100" height="100"/> 
    	        <p><span class="style1">Track Titles:<br /> 
    	        <h3> 
    	        <ol>1 - The Song Remains The Same</ol>  
    	        <ol>2 - The Rain Song</ol> 
    	        <ol>3 - Over The Hills And Far Away</ol>  
    	        <ol>4 - The Crunge</ol>  
    	        <ol>5 - Dancing Daysv</ol>
    	        <ol>6 - D yer Mak er</ol>  
    	        <ol>7 - No Quarter</ol>  
    	        <ol>8 - The Ocean</ol> 
    	        </h3> 
    	        <br /><iframe title="YouTube video player" width="480" height="290" src="http://www.youtube.com/embed/D3vs9y6CMrM" frameborder="0" allowfullscreen></iframe> 
    	        </p> 
    	        </td> 
    	      </tr> 
    	    </table> 
    	    <?php  
    	   
    	      }  
    	elseif ($day == 6) {  
    	        $album = 'Eric Clapton'; 
    			?>
    			<table> 
    	          <tr> 
    	        <td> 
    	        <a href = index1.html>Back</a><br> 
    	        <h1>Eric Clapton,Jeff Beck,Jimmy Page</h1> 
    	        <h2>"Layla"</h2> 
    	        <img src="images/clapton.jpg" alt="Eric Clapton" width="100" height="100"/> 
    	        <p><span class="style1">Track Titles:<br /> 
    	           <h3> 
    	        	<ol>1. Signe</ol> 
    	         	<ol>2. Before You Accuse Me</ol>  
    	         	<ol>3. Hey Hey</ol> 
    	            <ol>4. Tears in Heaven</ol>  
    	            <ol>5. Lonely Stranger</ol> 
    	            <ol>6. Nobody Knows You When Youre Down and Out</ol>  
    	            <ol>7. Layla</ol> 
    	            <ol>8. Running on Faith</ol>  
    	            <ol>9. Walkin Blues</ol> 
    	            <ol>10. Alberta</ol> 
    	            <ol>11. San Francisco Bay Blues</ol>  
    	            <ol>12. Malted Milk</ol> 
    	            <ol>13. Old Love</ol> 
    	            <ol>14. Rollin and Tumblin</ol> 
    	        </h3> 
    	<br /> 
    	<iframe title="YouTube video player" width="480" height="290" src="http://www.youtube.com/embed/ICpxgxThG7s" frameborder="0" allowfullscreen></iframe> 
    	        </span> 
    	        <br /> 
    	        </p> 
    	        </td> 
    	          </tr> 
    	        </table>
    			<?php   
    	      }
    		  ?>   
    	  </body>  
    	</html>
  • Aimee Bailey
    Recognized Expert New Member
    • Apr 2010
    • 197

    #2
    Instead of giving you the answer which wouldnt really teach you much, here is a link to a very popular tutorial on the matter:

    Learn how to use MySQL with PHP in Tizag.com's MySQL and PHP lesson.


    I encourage you to read it, as it will show you everything you need to know.

    Aimee.

    Comment

    • liosbeg
      New Member
      • Apr 2011
      • 5

      #3
      Thanks Aimee.
      I have looked at this and started over again using code.
      Still having some problems although smaller now!

      Here is my new code. If it is something small I am doing can you let me know? Really appreciate your help.
      Code:
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
      <html xmlns="http://www.w3.org/1999/xhtml"> 
      <head> 
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> 
      <title>Album</title> 
      <link rel="stylesheet" type="text/css" href="homework.css"> 
      </head> 
      <body>  
      <?php  
      // get form selection  
      $day = $_GET['day']; 
            // check value and select appropriate item 
            if ($day == 1) { 
      $album = 'James Taylor'; 
      
      // set database server access variables: 
            $host = "testweb2.csis.ul.ie"; //localhost on your home PC/laptop
            $user = "xxxxx"; //your ID number here
            $pass = "xxxx"; //your password here
            $db = "xxxxx"; //your ID number here
      
            // open connection 
            $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); 
      
      
      // Get a specific result from the "Tracks" table
      $result = mysql_query("SELECT * FROM Tracks
       WHERE album_number='1'") or die(mysql_error());  
      
      // get the first (and hopefully only) entry from the result
      $row = mysql_fetch_array( $result );
      // Print out the contents of each row into a table 
      echo $row['track_number']." - ".$row['track_title'];
      ?>
      
      <?php 
      }  
      elseif ($day == 2) { 
      $album = 'The Rolling Stones';  
      
      // set database server access variables: 
            $host = "testweb2.csis.ul.ie"; //localhost on your home PC/laptop
            $user = "xxxxxxx"; //your ID number here
            $pass = "xxxxxx"; //your password here
            $db = "xxxxxx"; //your ID number here
      
            // open connection 
            $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); 
      
      
      // Get a specific result from the "Tracks" table
      $result = mysql_query("SELECT * FROM Tracks
       WHERE album_number='2'") or die(mysql_error());  
      
      // get the first (and hopefully only) entry from the result
      $row = mysql_fetch_array( $result );
      // Print out the contents of each row into a table 
      echo $row['track_number']." - ".$row['track_title'];
      ?>
      
      
      <?php  
      }  
      elseif ($day == 3) {  
       $album = 'Sting';  
      
      // set database server access variables: 
            $host = "testweb2.csis.ul.ie"; //localhost on your home PC/laptop
            $user = "xxxxxxx"; //your ID number here
            $pass = "xxxxxxx"; //your password here
            $db = "xxxxxx"; //your ID number here
      
            // open connection 
            $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); 
      
      
      // Get a specific result from the "Tracks" table
      $result = mysql_query("SELECT * FROM Tracks
       WHERE album_number='3'") or die(mysql_error());  
      
      // get the first (and hopefully only) entry from the result
      $row = mysql_fetch_array( $result );
      // Print out the contents of each row into a table 
      echo $row['track_number']." - ".$row['track_title'];
      ?>
      
      <?php  
       }  
      elseif ($day == 4) {  
      $album = 'Bob Dylan'; 
      
      // set database server access variables: 
            $host = "testweb2.csis.ul.ie"; //localhost on your home PC/laptop
            $user = "xxxxxx"; //your ID number here
            $pass = "xxxxx"; //your password here
            $db = "xxxxxxx"; //your ID number here
      
            // open connection 
            $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); 
      
      
      // Get a specific result from the "Tracks" table
      $result = mysql_query("SELECT * FROM Tracks
       WHERE album_number='4'") or die(mysql_error());  
      
      // get the first (and hopefully only) entry from the result
      $row = mysql_fetch_array( $result );
      // Print out the contents of each row into a table 
      echo $row['track_number']." - ".$row['track_title'];
      ?>
      
      <?php  
      }  
      elseif ($day == 5) {  
      $album = 'Robert Plant and Jimmy Page';  
      
      // set database server access variables: 
            $host = "testweb2.csis.ul.ie"; //localhost on your home PC/laptop
            $user = "xxxxx"; //your ID number here
            $pass = "xxxxxx"; //your password here
            $db = "xxxxxx"; //your ID number here
      
            // open connection 
            $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); 
      
      
      // Get a specific result from the "Tracks" table
      $result = mysql_query("SELECT * FROM Tracks
       WHERE album_number='5'") or die(mysql_error());  
      
      // get the first (and hopefully only) entry from the result
      $row = mysql_fetch_array( $result );
      // Print out the contents of each row into a table 
      echo $row['track_number']." - ".$row['track_title'];
      ?>
      
      	    <?php  
      	   
      	      }  
      	elseif ($day == 6) {  
      	        $album = 'Eric Clapton'; 
      			
      // set database server access variables: 
            $host = "testweb2.csis.ul.ie"; //localhost on your home PC/laptop
            $user = "xxxxxx"; //your ID number here
            $pass = "xxxxxx"; //your password here
            $db = "xxxxxx"; //your ID number here
      
            // open connection 
            $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); 
      
      
      // Get a specific result from the "Tracks" table
      $result = mysql_query("SELECT * FROM Tracks
       WHERE album_number='6'") or die(mysql_error());  
      
      // get the first (and hopefully only) entry from the result
      $row = mysql_fetch_array( $result );
      // Print out the contents of each row into a table 
      echo $row['track_number']." - ".$row['track_title'];
      ?>
      			<?php   
      	      }
      		 ?>   
      	</body>  
      </html>

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        Still having some problems although smaller now!
        what smaller problems do you have? from my personal point-of-view there is a lot to do (starting with not using the outdated mysql functions), but that’s not something to throw at you yet.

        Comment

        Working...