Hi guys,

I've written a simple script to get items from a MySQL database and put them within the Spry Accordion widget supplied with Dreamweaver, but when I try to open the file the screen just comes up blank. Here is the code, saved in my site directory as index.php:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Rolling Snowballs</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<script src="Scripts/AC_RunActiveContent.js" type="text/javascript"></script>
<script src="SpryAssets/SpryAccordion.js" type="text/javascript"></script>

<link href="SpryAssets/SpryAccordion.css" rel="stylesheet" type="text/css" />

</head>

<body>
<div class="wrapper">


  <div class="bannertop"><br />
    <img src="images/title.png" width=219 height=35 alt="logo"/>    
</div> <div class="navbg"> <div id="navcontainer"> <ul id="navlist"> <li><a href=index2.html>Home</a></li> <li><a href="about.htm">About</a></li> <li><a href="news.htm">News</a></li> <li><a href="productions.htm">Productions</a></li> <li><a href="press.htm">Press</a> </li><li><a href="contact.htm">Contact</a></li> </ul> </div> </div> <div class="flashcontent"> <script type="text/javascript"> AC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0','width','800','height','150','title','browser','src','images/browser','quality','high','pluginspage','http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash','movie','images/browser' ); //end AC code </script><noscript><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="800" height="150" title="browser"> <param name="movie" value="images/browser.swf" /> <param name="quality" value="high" /> <embed src="images/browser.swf" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="800" height="150"></embed> </object></noscript> </div> <div class="content"> <div class="leftcolumn"> <div class="leftcolumnheader">Recent News </div> <div id="Accordion1" class="Accordion" tabindex="0"> <?php //connect to the server $connection = mysql_connect('localhost', 'guest','guest') or die ('Unable to connect to MySQL server'); // select database mysql_select_db('rollsnow') or die ('Unable to connect to Database!'); $query = 'SELECT date, title, info FROM news ORDER BY date DESC LIMIT 0 , 3;'; $result = mysql__query($query) or die ('Error in query: $query. ' . mysql_error()); if (mysql_num_rows($result) > 0 { while($row = mysql_fetch_row($result) { echo '<div class="AccordionPanel">'; echo '<div class="AccordionPanelTab">' . $row['date'] . ' - ' . $row['title'] . '</div>'; echo '<div class="AccordionPanelContent">' $row['info'] . '</div></div>'; } else { //print error message echo 'No Rows Found!'; } // free result set mysql_free_result($result); // close connection mysql_close($connection); ?> </div> </div> <div class="rightcolumn"> <div class="videobox"> <script type="text/javascript"> AC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0','width','337','height','252','src','images/deathvdeath','quality','high','pluginspage','http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash','movie','images/deathvdeath' ); //end AC code </script> <noscript> <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="337" height="252"> <param name="movie" value="images/deathvdeath.swf" /> <param name="quality" value="high" /> <embed src="images/deathvdeath.swf" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="337" height="252"></embed> </object> </noscript> </P> </div> </div> </div> </div> <script type="text/javascript"> <!-- var Accordion1 = new Spry.Widget.Accordion("Accordion1"); //--> </script> </body> </html>

Anybody have any ideas why this is?

Thanks
Edd

    p.s. have just realised the superfluous ";" in the mysql_query:
    $query = 'SELECT date, title, info FROM news ORDER BY date DESC LIMIT 0 , 3;';
    and have corrected it, but to no avail - it still just comes up with a blank screen.

    latest version of the code:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Rolling Snowballs</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
    <script src="Scripts/AC_RunActiveContent.js" type="text/javascript"></script>
    <script src="SpryAssets/SpryAccordion.js" type="text/javascript"></script>
    
    <link href="SpryAssets/SpryAccordion.css" rel="stylesheet" type="text/css" />
    
    </head>
    
    <body>
    <div class="wrapper">
    
    
      <div class="bannertop"><br />
        <img src="images/title.png" width=219 height=35 alt="logo"/>    
    </div> <div class="navbg"> <div id="navcontainer"> <ul id="navlist"> <li><a href=index2.html>Home</a></li> <li><a href="about.htm">About</a></li> <li><a href="news.htm">News</a></li> <li><a href="productions.htm">Productions</a></li> <li><a href="press.htm">Press</a> </li><li><a href="contact.htm">Contact</a></li> </ul> </div> </div> <div class="flashcontent"> <script type="text/javascript"> AC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0','width','800','height','150','title','browser','src','images/browser','quality','high','pluginspage','http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash','movie','images/browser' ); //end AC code </script><noscript><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="800" height="150" title="browser"> <param name="movie" value="images/browser.swf" /> <param name="quality" value="high" /> <embed src="images/browser.swf" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="800" height="150"></embed> </object></noscript> </div> <div class="content"> <div class="leftcolumn"> <div class="leftcolumnheader">Recent News </div> <div id="Accordion1" class="Accordion" tabindex="0"> <?php //connect to the server $connection = mysql_connect('localhost', 'guest','guest') or die ('Unable to connect to MySQL server'); // select database mysql_select_db('rollsnow') or die ('Unable to connect to Database!'); $query = 'SELECT date, title, info FROM news ORDER BY date DESC LIMIT 0 , 3'; $result = mysql__query($query) or die ('Error in query: $query. ' . mysql_error()); if (mysql_num_rows($result) > 0 { while($row = mysql_fetch_row($result) { echo '<div class="AccordionPanel">'; echo '<div class="AccordionPanelTab">' . $row['date'] . ' - ' . $row['title'] . '</div>'; echo '<div class="AccordionPanelContent">' $row['info'] . '</div></div>'; } else { //print error message echo 'No Rows Found!'; } // free result set mysql_free_result($result); // close connection mysql_close($connection); ?> </div> </div> <div class="rightcolumn"> <div class="videobox"> <script type="text/javascript"> AC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0','width','337','height','252','src','images/deathvdeath','quality','high','pluginspage','http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash','movie','images/deathvdeath' ); //end AC code </script> <noscript> <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="337" height="252"> <param name="movie" value="images/deathvdeath.swf" /> <param name="quality" value="high" /> <embed src="images/deathvdeath.swf" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="337" height="252"></embed> </object> </noscript> </P> </div> </div> </div> </div> <script type="text/javascript"> <!-- var Accordion1 = new Spry.Widget.Accordion("Accordion1"); //--> </script> </body> </html>

      Your mistake here is in that mysql_fetch_row returns an ENUMERATED array: ie the array elements are indexed numerically and must be accessed as such

                  echo '<div class="AccordionPanelTab">' . $row[0] . ' - ' . $row[1]
      

      If you want to access the data array by name then use mysql_fetch_array or mysql_fetch_assoc

      Personally I always use fetch_array as it defaults to both numeric and associative

        Hi There,

        Thanks for the reply! Hmm - have tried both ways round but I still get a blank screen - either way I should have some dividers right? but the source code turns up completely blank. There were quite a few other errors I've discovered, but I think I've got them all now and it doesn't work - here's the most up-to-date php:

         <?php
        
        //connect to the server
        $connection = mysql_connect('localhost', 'guest','guest')
        or die ('Unable to connect to MySQL server');
        
        // select database
        mysql_select_db('rollsnow') or die ('Unable to connect to Database!');
        
        $query = 'SELECT date, title, info FROM news ORDER BY date DESC LIMIT 0 , 3';
        $result = mysql_query($query)
        or die ('Error in query: $query. ' . mysql_error());
        
        if (mysql_num_rows($result) > 0) {
        
        	while ($row = mysql_fetch_row($result))
        	{
        
        		echo '<div class="AccordionPanel">';
        		echo '<div class="AccordionPanelTab">' . $row[0] . ' - ' . $row[1] . '</div>';
        		echo '<div class="AccordionPanelContent">' $row[2] . '</div></div>';
        	}
        
        }
        else
        {
        	//print error message
        	echo 'No Rows Found!';
        }
        
        // free result set
        mysql_free_result($result);
        
        // close connection
        mysql_close($connection);
        ?>

          Personally, I always use mysql_fetch_assoc() since I always refer to the row elements by name, to maximize code readability and to make sure I'm getting the column I intended to get. (Besides, if I have to count enumerated array indexes, I always get confused since I can never decide which finger to use for the 0th index. 😉 )

            Oops strike that - I see you have cleaned up the typos.

            Now what is appearing in the page source? Are you getting the empty <div> tags?

            Do some basic debugging by adding echos or prints

            $rowcount = mysql_num_rows($result);
            echo 'rowcount = ' . $rowcount . '<br />';
            
            if ($rowcount > 0) {
            
                while ($row = mysql_fetch_row($result))
                {
            
                    echo '<div class="AccordionPanel">';
                    echo '<div class="AccordionPanelTab">' . $row[0] . ' - ' . $row[1] . '</div>';
                    echo '<div class="AccordionPanelContent">' $row[2] . '</div></div>';
                } 
            

            Use stuff like that to track the processing during development and comment out or remove in production version.

              Wow! great tip thanks! Still haven't quite worked it out though - I know the area of code that's at fault though. Here's the debug code:

               <?php
              
              //connect to the server
              $connection = mysql_connect('localhost', 'guest','guest')
              or die ('Unable to connect to MySQL server');
              
              echo 'Connected to Server<BR>';
              
              // select database
              mysql_select_db('rollsnow') or die ('Unable to connect to Database!<BR>');
              
              echo 'Connected to Database<BR>';
              
              
              $query = 'SELECT date, title, info FROM news ORDER BY date DESC LIMIT 0 , 3';
              
              echo $query . '<BR>';
              
              $result = mysql_query($query)
              or die ('Error in query: $query. ' . mysql_error());
              
              print_r($result);
              
              echo '<BR>';
              
              $rows = mysql_num_rows($result);
              echo 'Rows: ' . $rows . '<br>';
              
              
              if (mysql_num_rows($result) > 0) {
              
              	echo 'There are ' . $rows . 'rows present<br>';
              
              	while ($row = mysql_fetch_row($result))
              	{
              
              		echo '<div class="AccordionPanel">';
              		echo '<div class="AccordionPanelTab">' . $row[0] . ' - ' . $row[1] . '</div>';
              		echo '<div class="AccordionPanelContent">' $row[2] . '</div></div>';
              	}
              
              }
              else
              {
              	//print error message
              	echo 'No Rows Found!';
              }
              
              // free result set
              mysql_free_result($result);
              
              // close connection
              mysql_close($connection);
              
              ?>
              
              

              It's this part that's wrong:

              echo '<div class="AccordionPanel">';
              			echo '<div class="AccordionPanelTab">' . $row[0] . ' - ' . $row[1] . '</div>';
              			echo '<div class="AccordionPanelContent">' $row[2] . '</div></div>';

              I could not tell you why though - non of my debugging code comes up when I keep this code in - I had to selectively delete parts of the script until I found it out. Any ideas? replacing it with this worked fine:

              echo $row[0] . ' ' . $row[1] . ' ' . $row[2]. '<BR>';

              Thanks guys, this forum's great!

              Edd

                ...and this kinda fixes it:

                ?>
                
                        <div class="AccordionPanel">
                        <div class="AccordionPanelContent"><?php echo $row[2]; ?></div>
                        <div class="AccordionPanelTab"><?php echo $row[0] . ' - ' . $row[1]; ?> </div>
                        </div></div>
                
                        <?php

                I wish I knew the original didn't work though. I got it down to just one line:

                echo '<div class="AccordionPanelContent">' $row[2] . '</div>';

                I wonder if it was to do with the text in $row[2]. Hmmm...

                Edd:-)

                  Ahhh! Have just worked it out. Missing period in:

                  echo '<div class="AccordionPanelContent">' $row[2] . '</div>';

                  Should read

                  echo '<div class="AccordionPanelContent">' . $row[2] . '</div>';

                  DOH! Thanks guys for your help!

                  Edd

                    Write a Reply...