473,387 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

alpnz
113 100+
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.

Expand|Select|Wrap|Line Numbers
  1. <?
  2. $host = "localhost";
  3. $user = "root";
  4. $pass = "";
  5. $dbname = "wxoww";
  6. $datatbl= "tbl_owwdata";
  7.  
  8. $connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error());
  9.  
  10. mysql_select_db ($dbname);
  11.  
  12. $sql = ("SELECT * FROM $datatbl ORDER BY qtm LIMIT 1") ;
  13.  
  14. $query = mysql_query($sql);
  15. echo'<table border="0" width="100%">';
  16. while ($row = mysql_fetch_array($query)) {
  17. echo'<tr>
  18.      <td width="8%">'.$row["qtm"].'</td>
  19.     <td width="8%">'.$row["t1"].'</td>
  20.     <td width="8%">'.$row["t2"].'</td>
  21.     <td width="8%">'.$row["dp"].'</td>
  22.     <td width="8%">'.$row["rh"].'</td>
  23.     <td width="8%">'.$row["bp"].'</td>
  24.     <td width="8%">'.$row["wdspd"].'</td>
  25.     <td width="8%">'.$row["wdgst"].'</td>
  26.     <td width="8%">'.$row["wddir"].'</td>
  27.     <td width="8%">'.$row["rn"].'</td>
  28.     <td width="8%">'.$row["rnd"].'</td>
  29.     <td width="8%">'.$row["rnrt"].'</td>
  30. </tr>' 
  31.  
  32. echo'</table>';
  33.  
  34. ?> 
  35.  
The resulting page if I bring it up in a browser has this on it
Expand|Select|Wrap|Line Numbers
  1. '; 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'';  ?>  
  2.  
It suggests to me that the $query is at fault, but the sql query is correct I think ... duh isn't it ...??



Many thanks
Dec 15 '08 #1
7 2120
dumm
10
If you get this type of output to the browser, try to put <?php instead of <?
Dec 16 '08 #2
alpnz
113 100+
I get an empty screen with this, what have I missed
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $host = "localhost";
  3. $user = "wxoww";
  4. $pass = "password";
  5. $dbname = "wxoww";
  6. $datatbl= "tbl_owwdata";
  7.  
  8. $connection = mysql_connect($host,$user,$pass);
  9.  
  10. if(!$connection) ""  {
  11.   die('Could not connect: ' . mysql_error());
  12.   }
  13.  
  14. mysql_select_db($dbname);
  15.  
  16. $sql =("SELECT * FROM $datatbl ORDER BY qtm DESC LIMIT 1");
  17.  
  18. $query = mysql_query($sql);
  19.  
  20. while($row = mysql_fetch_array($query)) {
  21.         echo $row['qtm']" "$row['t1']" "$row['t2'];
  22.         }
  23. ?> 
  24.  
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.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $host = "localhost" ;
  3. $user = "wxoww" ; 
  4. $pass = "zaq11qaz" ; 
  5. $db = "wxoww"; 
  6. $dbf = "tbl_owwdata" ; 
  7.  
  8. // Parse query string from URL and put into an array
  9. // This part fetches the part after the "?" from the URL.
  10. $data = "$_SERVER[QUERY_STRING]";
  11.  
  12. // echo "$data <br />"; //test returns query string
  13.  
  14. //Break query string into parts delimited by the "&" (key=val)
  15.  
  16. $query_string = explode( '&', $data );
  17. // echo "$query_string <br />"; //test returns "Array"
  18.  
  19. $args = array( ); // return array
  20. // echo "$args <br />" //test returns "Array"
  21.  
  22. //Loop through the array, break at "=",
  23.  
  24. foreach( $query_string as $chunk )
  25. {
  26. $chunk = explode( '=', $chunk );
  27.  
  28. list( $key, $val ) = $chunk;
  29.  
  30. // echo "$key $val <br />"; //test returns keys and their assosiated values
  31.  
  32. /*
  33. ****************************************************
  34. For each iteration of the loop test for key, create 
  35. a variable and set its value to the one associated with it.
  36. These values are used to update the database. 
  37. ***************************************************
  38. */
  39.  
  40. switch ($key):
  41.  
  42. case ($key == "qtm"):
  43. global $qtm;
  44. $qtm = $val;
  45. break;
  46.  
  47. case ($key == "t1"):
  48. global $t1;
  49. $t1 = $val;
  50. break;
  51.  
  52. case ($key == "t2"):
  53. global $t2;
  54. $t2 = $val;
  55. break;
  56.  
  57. case ($key == "t3"):
  58. global $t3;
  59. $t3 = $val;
  60. break;
  61.  
  62. case ($key == "dp"):
  63. global $dp;
  64. $dp = $val;
  65. break;
  66.  
  67. case ($key == "rh"):
  68. global $rh;
  69. $rh = $val;
  70. break;
  71.  
  72. case ($key == "bp"):
  73. global $bp;
  74. $bp = $val;
  75. break;
  76.  
  77. case ($key == "wdspd"):
  78. global $wdspd;
  79. $wdspd = $val;
  80. break;
  81.  
  82. case ($key == "wdgst"):
  83. global $wdgst;
  84. $wdgst = $val;
  85. break;
  86.  
  87. case ($key == "wddir"):
  88. global $wddir;
  89. $wddir = $val;
  90. break;
  91.  
  92. case ($key == "wddeg"):
  93. global $wddeg;
  94. $wddeg = $val;
  95. break;
  96.  
  97. case ($key == "wdchill"):
  98. global $wdchill;
  99. $wdchill = $val;
  100. break;
  101.  
  102. case ($key == "wdpnt"):
  103. global $wdpnt;
  104. $wdpnt = $val;
  105. break;
  106.  
  107. case ($key == "rn"):
  108. global $rn;
  109. $rn = $val;
  110. break;
  111.  
  112. case ($key == "rnd"):
  113. global $rnd;
  114. $rnd = $val;
  115. break;
  116.  
  117. case ($key == "rnrt"):
  118. global $rnrt;
  119. $rnrt = $val;
  120. break;
  121.  
  122. case ($key == "rnint"):
  123. global $rnint;
  124. $rnint = $val;
  125. break;
  126.  
  127. case ($key == "c1"):
  128. global $c1;
  129. $c1 = $val;
  130. break;
  131.  
  132. case ($key == "cd1"):
  133. global $cd1;
  134. $cd1 = $val;
  135. break;
  136.  
  137. case ($key == "iadc1"):
  138. global $iadc1;
  139. $iadc1 = $val;
  140. break;
  141.  
  142. case ($key == "vadc1"):
  143. global $vadc1;
  144. $vadc1 = $val;
  145. break;
  146.  
  147. case ($key == "sol1"):
  148. global $sol1;
  149. $sol1 = $val;
  150. break;
  151.  
  152. endswitch;
  153. $args[ $key ] = urldecode( $val );
  154. }
  155. // connect to the database
  156. $con = mysql_connect("$host","$user","$pass");
  157. if (!$con)
  158. {
  159. die('Could not connect: ' . mysql_error());
  160. }
  161. // select the database
  162. mysql_select_db("$db", $con) or die ("error selecting db");
  163. // Insert values into the fields in the database
  164. 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')");
  165.  
  166. mysql_close($con); 
  167.  
  168. ?>
  169.  
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.
Dec 23 '08 #3
Markus
6,050 Expert 4TB
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.
Dec 24 '08 #4
Atli
5,058 Expert 4TB
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.
Dec 24 '08 #5
alpnz
113 100+
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.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. error_reporting(E_ALL);
  3. ini_set('display_errors', true);
  4.  
  5. $host = "localhost";
  6. $user = "wxoww";
  7. $pass = "password";
  8. $dbname = "wxoww";
  9. $datatbl= "tbl_owwdata";
  10.  
  11. $connection = mysql_connect($host,$user,$pass);
  12.  
  13. if(!$connection) {
  14.   die('Could not connect: ' . mysql_error());
  15.   }
  16.  
  17. mysql_select_db($dbname);
  18.  
  19. $sql =("SELECT * FROM $datatbl ORDER BY qtm DESC LIMIT 1");
  20.  
  21. $query = mysql_query($sql);
  22. while($dd = mysql_fetch_array($query))
  23. {
  24. //echo <table border=1 align=center>
  25. //    echo <tr>;
  26. //    echo <td>$dd['qtm']</td>;
  27. //    echo <td>$dd['t1']</td>;
  28. //    </tr>;
  29. // echo </table>; 
  30.  
  31. 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'];
  32. echo "<br />";
  33. }
  34.  
  35.  
  36. ?> 
  37.  
@Atli
Dec 24 '08 #6
Atli
5,058 Expert 4TB
That would probably be because your strings are not quoted.
Expand|Select|Wrap|Line Numbers
  1. // This is incorrect:
  2. echo <div class="whatever">$row['something']</h1>;
  3.  
  4. // It should be:
  5. echo '<div class="whatever">'. $row['something'] .'</h1>';
  6.  
  7. // Or: (Note how the quotes that are meant to be printed are escaped)
  8. echo "<div class=\"whatever\">{$row['something']}</h1>";
  9.  
  10. // Or if you have a lot of output, you can also do:
  11. echo <<<HTML
  12. <div class="whatever">{$row['something']}</div>
  13. <span>Some more HTML</span>
  14. <span>etc...</span>
  15. HTML;
  16.  
You could probably avoid all these basic syntax errors if you read through a good PHP tutorial real quick. Like say, this one.
Dec 24 '08 #7
alpnz
113 100+
Many thanks for the advice. Below is the resulting code
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. error_reporting(E_ALL);
  3. ini_set('display_errors', true);
  4. $host = "localhost";
  5. $user = "wxoww";
  6. $pass = "password";
  7. $dbname = "wxoww";
  8. $datatbl= "tbl_owwdata";
  9. $connection = mysql_connect($host,$user,$pass);
  10. if(!$connection) {
  11. die('Could not connect: ' . mysql_error());
  12. }
  13. mysql_select_db($dbname);
  14. $sql =("SELECT * FROM $datatbl ORDER BY qtm DESC LIMIT 20");
  15. $query = mysql_query($sql);
  16. echo "<table class=\"sample\">";
  17. echo "<th width =\"12%\">Date/Time</th>";
  18. echo "<th width =\"5%\">Temp</th>";
  19. echo "<th width =\"5%\">Indoor</th>";
  20. echo "<th width =\"5%\">Dew</th>";
  21. echo "<th width =\"5%\">Humid</th>";
  22. echo "<th width =\"5%\">Baro</th>";
  23. echo "<th width =\"5%\">Wind</th>";
  24. echo "<th width =\"5%\">Gust</ch>";
  25. echo "<th width =\"5%\">Direction</th>";
  26. echo "<th width =\"5%\">Rain</ch>";
  27. echo "<th width =\"5%\">Daily mm</th>";
  28. echo "<th width =\"5%\">Intensity/Hr</th>";
  29. while($dd = mysql_fetch_array($query))
  30. {
  31. echo "<p class=\"sample\">";
  32. echo "<tr>";
  33. echo "<td width =\"12%\">$dd[qdat]</td>";
  34. echo "<td width =\"5%\">$dd[t1]</td>";
  35. echo "<td width =\"5%\">$dd[t3]</td>";
  36. echo "<td width =\"5%\">$dd[dp]</td>";
  37. echo "<td width =\"5%\">$dd[rh]</td>";
  38. echo "<td width =\"5%\">$dd[bp]</td>";
  39. echo "<td width =\"5%\">$dd[wdspd]</td>";
  40. echo "<td width =\"5%\">$dd[wdgst]</td>";
  41. echo "<td width =\"5%\">$dd[wddir]</td>";
  42. echo "<td width =\"5%\">$dd[rn]</td>";
  43. echo "<td width =\"5%\">$dd[rnd]</td>";
  44. echo "<td width =\"5%\">$dd[rnrt]</td>";
  45. echo "</tr>";
  46. echo "</p>";
  47. }
  48. echo "</table>";
  49.  
  50. ?>
  51.  
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.
Dec 29 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: F. Biguet | last post by:
Hello, I want to create a generic script that connects to Oracle databases (from 7.3.4 to 9.2 version) with a guest user. This script should return 1 if connection is successful and 0 if not....
1
by: Randell D. | last post by:
HELP! I am determined to stick with this... I'm getting there... for those who haven't read my earlier posts, I'm createing what should be a simple function that I can call to check that...
6
by: Kenneth | last post by:
Hello, I'm having some serious problems debugging a script that I'm trying to make work. I'm working on a form where a user can type in a time (in the format of HH:MM), and another script...
4
by: nielsonj1976 | last post by:
I am getting a failure on the db backup job of one of my maintenance plans. It is coming back with the generic error message of, "sqlmaint.exe failed. (Error 22029). The step failed." I...
8
by: cy | last post by:
Hi all, I would appreciate it if anyone could point me on this. My JSP page has 3 select boxes for states, counties and, cities and the counties and cities should be populated once the user...
5
by: calaha | last post by:
Hi all, I have been working with this since last night, and can't quite figure out why it's not working. I have a simple login box form that is set to be my startup form in my Access app (upon...
2
by: MLH | last post by:
I cut a mail function off the m'soft site. Has always worked. However, I would like to include error codes returned by the sendmail Fn and be able to understand what they mean. I had my first...
3
by: Guy Debord | last post by:
Hello all, I know that this is a long shot, but I have a problem which someone reading this group *may* just be able to shed some light on. We have a new internal personnel planner/attendance...
0
by: andrewcw | last post by:
I have tried : http://msdn2.microsoft.com/en-us/library/system.management.connectionoptions.aspx using my own credentials - no options supplied and as known alternate user But it simply gives an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.