Simple script failure ... any clues for a no clues user

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alpnz
    New Member
    • Nov 2006
    • 113

    Simple script failure ... any clues for a no clues user

    I am trying to access data on a mySQL server, but due to my age and alzheimer's I seem to be overlooking the obvious mistake here. Anyone care to spare a couple of minutes to help.

    Code:
    <?
    $host = "localhost";
    $user = "root";
    $pass = "";
    $dbname = "wxoww";
    $datatbl= "tbl_owwdata";
    
    $connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error());
    
    mysql_select_db ($dbname);
    
    $sql = ("SELECT * FROM $datatbl ORDER BY qtm LIMIT 1") ;
    
    $query = mysql_query($sql);
    echo'<table border="0" width="100%">';
    while ($row = mysql_fetch_array($query)) {
    echo'<tr>
         <td width="8%">'.$row["qtm"].'</td>
        <td width="8%">'.$row["t1"].'</td>
        <td width="8%">'.$row["t2"].'</td>
        <td width="8%">'.$row["dp"].'</td>
        <td width="8%">'.$row["rh"].'</td>
        <td width="8%">'.$row["bp"].'</td>
        <td width="8%">'.$row["wdspd"].'</td>
        <td width="8%">'.$row["wdgst"].'</td>
        <td width="8%">'.$row["wddir"].'</td>
        <td width="8%">'.$row["rn"].'</td>
        <td width="8%">'.$row["rnd"].'</td>
        <td width="8%">'.$row["rnrt"].'</td>
    </tr>' 
    } 
    
    echo'</table>';
    
    ?>
    The resulting page if I bring it up in a browser has this on it
    Code:
    '; while ($row = mysql_fetch_array($query)) { echo'  	'.$row["qtm"].' 	'.$row["t1"].' 	'.$row["t2"].' 	'.$row["dp"].' 	'.$row["rh"].' 	'.$row["bp"].' 	'.$row["wdspd"].' 	'.$row["wdgst"].' 	'.$row["wddir"].' 	'.$row["rn"].' 	'.$row["rnd"].' 	'.$row["rnrt"].' '  }   echo'';  ?>
    It suggests to me that the $query is at fault, but the sql query is correct I think ... duh isn't it ...??



    Many thanks
    Last edited by Markus; Dec 16 '08, 12:47 AM. Reason: fixed [code] tags.
  • dumm
    New Member
    • Dec 2008
    • 10

    #2
    If you get this type of output to the browser, try to put <?php instead of <?

    Comment

    • alpnz
      New Member
      • Nov 2006
      • 113

      #3
      Still no joy,

      I get an empty screen with this, what have I missed
      Code:
      <?php
      $host = "localhost";
      $user = "wxoww";
      $pass = "password";
      $dbname = "wxoww";
      $datatbl= "tbl_owwdata";
      
      $connection = mysql_connect($host,$user,$pass);
      
      if(!$connection) ""  {
        die('Could not connect: ' . mysql_error());
        }
      
      mysql_select_db($dbname);
      
      $sql =("SELECT * FROM $datatbl ORDER BY qtm DESC LIMIT 1");
      
      $query = mysql_query($sql);
      
      while($row = mysql_fetch_array($query)) {
              echo $row['qtm']" "$row['t1']" "$row['t2'];
              }
      ?>
      Essentially what this returns is the last record in the database, the sql query works fine in other query browser type applications but not in the php file on the server.

      I have a file wxdataload.php which inserts the data into the mysql database just fine, using an output parser from an application called OWW.
      Code:
      <?php
      $host = "localhost" ;
      $user = "wxoww" ; 
      $pass = "zaq11qaz" ; 
      $db = "wxoww"; 
      $dbf = "tbl_owwdata" ; 
      
      // Parse query string from URL and put into an array
      // This part fetches the part after the "?" from the URL.
      $data = "$_SERVER[QUERY_STRING]";
      
      // echo "$data <br />"; //test returns query string
      
      //Break query string into parts delimited by the "&" (key=val)
      
      $query_string = explode( '&', $data );
      // echo "$query_string <br />"; //test returns "Array"
      
      $args = array( ); // return array
      // echo "$args <br />" //test returns "Array"
      
      //Loop through the array, break at "=",
      
      foreach( $query_string as $chunk )
      {
      $chunk = explode( '=', $chunk );
      
      list( $key, $val ) = $chunk;
      
      // echo "$key $val <br />"; //test returns keys and their assosiated values
      
      /*
      ****************************************************
      For each iteration of the loop test for key, create 
      a variable and set its value to the one associated with it.
      These values are used to update the database. 
      ***************************************************
      */
      
      switch ($key):
      
      case ($key == "qtm"):
      global $qtm;
      $qtm = $val;
      break;
      
      case ($key == "t1"):
      global $t1;
      $t1 = $val;
      break;
      
      case ($key == "t2"):
      global $t2;
      $t2 = $val;
      break;
      
      case ($key == "t3"):
      global $t3;
      $t3 = $val;
      break;
      
      case ($key == "dp"):
      global $dp;
      $dp = $val;
      break;
      
      case ($key == "rh"):
      global $rh;
      $rh = $val;
      break;
      
      case ($key == "bp"):
      global $bp;
      $bp = $val;
      break;
      
      case ($key == "wdspd"):
      global $wdspd;
      $wdspd = $val;
      break;
      
      case ($key == "wdgst"):
      global $wdgst;
      $wdgst = $val;
      break;
      
      case ($key == "wddir"):
      global $wddir;
      $wddir = $val;
      break;
      
      case ($key == "wddeg"):
      global $wddeg;
      $wddeg = $val;
      break;
      
      case ($key == "wdchill"):
      global $wdchill;
      $wdchill = $val;
      break;
      
      case ($key == "wdpnt"):
      global $wdpnt;
      $wdpnt = $val;
      break;
      
      case ($key == "rn"):
      global $rn;
      $rn = $val;
      break;
      
      case ($key == "rnd"):
      global $rnd;
      $rnd = $val;
      break;
      
      case ($key == "rnrt"):
      global $rnrt;
      $rnrt = $val;
      break;
      
      case ($key == "rnint"):
      global $rnint;
      $rnint = $val;
      break;
      
      case ($key == "c1"):
      global $c1;
      $c1 = $val;
      break;
      
      case ($key == "cd1"):
      global $cd1;
      $cd1 = $val;
      break;
      
      case ($key == "iadc1"):
      global $iadc1;
      $iadc1 = $val;
      break;
      
      case ($key == "vadc1"):
      global $vadc1;
      $vadc1 = $val;
      break;
      
      case ($key == "sol1"):
      global $sol1;
      $sol1 = $val;
      break;
      
      endswitch;
      $args[ $key ] = urldecode( $val );
      }
      // connect to the database
      $con = mysql_connect("$host","$user","$pass");
      if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
      // select the database
      mysql_select_db("$db", $con) or die ("error selecting db");
      // Insert values into the fields in the database
      mysql_query("INSERT INTO $dbf (qtm, t1, t2, t3, dp, rh, bp, wdspd, wdgst, wddir, wddeg, wdchill, wdpnt, rn, rnd, rnrt, rnint, c1, cd1, iadc1, vadc1, sol1 ) VALUES ('$qtm','$t1','$t2','$t3','$dp','$rh','$bp','$wdspd','$wdgst','$wddir','$wddeg','$wdchill','$wdpnt','$rn','$rnd','$rnrt','$rnint','$c1','$cd1','$iadc1','$vadc1','$sol1')");
      
      mysql_close($con); 
      
      ?>
      The output parser was adapted from code supplied by others in the weather data community.
      What I wish to do is create a web page that can read the data from the Database server. Not having a lot of luck. Any one know of a good data based web development tutorial for me to get stuck into.

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Don't have time to look over your problem, but regarding some tutorials:

        MySQL Tutorial - Introduction

        PHP MySQL Introduction

        Good luck and Merry Christmas,

        Markus.

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Hi.

          Just to explain dumm's suggestion.
          The short-tags you using in your original example, <? ... ?>, are not enabled by default in PHP5. Therefore, unless you specifically configure your PHP installation to work with the short-tags, PHP will not interpret the code as PHP code and simply send it as plain text, rather than executing it.

          That is why you should always use the <?php ... ?> tags. They never fail.

          As to your code, there two things I see wrong with the code itself.
          The extra quote-marks you have after the if statement in line #10, and the incorrectly formatted string you try to echo in line #21.
          They should be producing an error.
          (Try turning on the debug messages)

          Once that is fixed, if the page continues to return no results, it would indicate that your database is in fact not returning any results.
          If that is the case, try printing the actual query your code executes before it is executed and see if it looks like it should look.

          Comment

          • alpnz
            New Member
            • Nov 2006
            • 113

            #6
            Many thanks ...

            Many thanks ...

            I got a basic line of data as per the plan however I would now like to present it in table form. This code produces the line, you will see that the commented out line however do not.

            Code:
            <?php
            error_reporting(E_ALL);
            ini_set('display_errors', true);
            
            $host = "localhost";
            $user = "wxoww";
            $pass = "password";
            $dbname = "wxoww";
            $datatbl= "tbl_owwdata";
            
            $connection = mysql_connect($host,$user,$pass);
            
            if(!$connection) {
              die('Could not connect: ' . mysql_error());
              }
            
            mysql_select_db($dbname);
            
            $sql =("SELECT * FROM $datatbl ORDER BY qtm DESC LIMIT 1");
            
            $query = mysql_query($sql);
            while($dd = mysql_fetch_array($query))
            {
            //echo <table border=1 align=center>
            //    echo <tr>;
            //    echo <td>$dd['qtm']</td>;
            //    echo <td>$dd['t1']</td>;
            //    </tr>;
            // echo </table>; 
            
            echo $dd['qtm'], $dd['t1'], $dd['t2'], $dd['t3'], $dd['dp'], $dd['rh'], $dd['bp'], $dd['wdspd'], $dd['wdgst'], $dd['wddir'], $dd['rn'], $dd['rnd'], $dd['rnrt'], $dd['iadc1'];
            echo "<br />";
            }
            
            
            ?>
            Originally posted by Atli
            Hi.

            Just to explain dumm's suggestion.
            The short-tags you using in your original example, <? ... ?>, are not enabled by default in PHP5. Therefore, unless you specifically configure your PHP installation to work with the short-tags, PHP will not interpret the code as PHP code and simply send it as plain text, rather than executing it.

            That is why you should always use the <?php ... ?> tags. They never fail.

            As to your code, there two things I see wrong with the code itself.
            The extra quote-marks you have after the if statement in line #10, and the incorrectly formatted string you try to echo in line #21.
            They should be producing an error.
            (Try turning on the debug messages)

            Once that is fixed, if the page continues to return no results, it would indicate that your database is in fact not returning any results.
            If that is the case, try printing the actual query your code executes before it is executed and see if it looks like it should look.

            Comment

            • Atli
              Recognized Expert Expert
              • Nov 2006
              • 5062

              #7
              That would probably be because your strings are not quoted.
              [code=php]
              // This is incorrect:
              echo <div class="whatever ">$row['something']</h1>;

              // It should be:
              echo '<div class="whatever ">'. $row['something'] .'</h1>';

              // Or: (Note how the quotes that are meant to be printed are escaped)
              echo "<div class=\"whateve r\">{$row['something']}</h1>";

              // Or if you have a lot of output, you can also do:
              echo <<<HTML
              <div class="whatever ">{$row['something']}</div>
              <span>Some more HTML</span>
              <span>etc...</span>
              HTML;
              [/code]
              You could probably avoid all these basic syntax errors if you read through a good PHP tutorial real quick. Like say, this one.

              Comment

              • alpnz
                New Member
                • Nov 2006
                • 113

                #8
                Many thanks for the advice it worked.

                Many thanks for the advice. Below is the resulting code
                Code:
                <?php
                error_reporting(E_ALL);
                ini_set('display_errors', true);
                $host = "localhost";
                $user = "wxoww";
                $pass = "password";
                $dbname = "wxoww";
                $datatbl= "tbl_owwdata";
                $connection = mysql_connect($host,$user,$pass);
                if(!$connection) {
                die('Could not connect: ' . mysql_error());
                }
                mysql_select_db($dbname);
                $sql =("SELECT * FROM $datatbl ORDER BY qtm DESC LIMIT 20");
                $query = mysql_query($sql);
                echo "<table class=\"sample\">";
                echo "<th width =\"12%\">Date/Time</th>";
                echo "<th width =\"5%\">Temp</th>";
                echo "<th width =\"5%\">Indoor</th>";
                echo "<th width =\"5%\">Dew</th>";
                echo "<th width =\"5%\">Humid</th>";
                echo "<th width =\"5%\">Baro</th>";
                echo "<th width =\"5%\">Wind</th>";
                echo "<th width =\"5%\">Gust</ch>";
                echo "<th width =\"5%\">Direction</th>";
                echo "<th width =\"5%\">Rain</ch>";
                echo "<th width =\"5%\">Daily mm</th>";
                echo "<th width =\"5%\">Intensity/Hr</th>";
                while($dd = mysql_fetch_array($query))
                {
                echo "<p class=\"sample\">";
                echo "<tr>";
                echo "<td width =\"12%\">$dd[qdat]</td>";
                echo "<td width =\"5%\">$dd[t1]</td>";
                echo "<td width =\"5%\">$dd[t3]</td>";
                echo "<td width =\"5%\">$dd[dp]</td>";
                echo "<td width =\"5%\">$dd[rh]</td>";
                echo "<td width =\"5%\">$dd[bp]</td>";
                echo "<td width =\"5%\">$dd[wdspd]</td>";
                echo "<td width =\"5%\">$dd[wdgst]</td>";
                echo "<td width =\"5%\">$dd[wddir]</td>";
                echo "<td width =\"5%\">$dd[rn]</td>";
                echo "<td width =\"5%\">$dd[rnd]</td>";
                echo "<td width =\"5%\">$dd[rnrt]</td>";
                echo "</tr>";
                echo "</p>";
                }
                echo "</table>";
                
                ?>
                An online page using this code in it can be found at Clyde Weather which will redirect to the page with the data on it.
                I have provided the above code that it might help anyone starting out as I am.

                Comment

                Working...