473,781 Members | 2,625 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

alpnz
113 New Member
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 2141
dumm
10 New Member
If you get this type of output to the browser, try to put <?php instead of <?
Dec 16 '08 #2
alpnz
113 New Member
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 Recognized Expert Expert
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 Recognized Expert Expert
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 New Member
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 Recognized Expert Expert
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 New Member
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
10390
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. My problem is due to the 3 requests for password when the connection fails. I can't find any option that allows to ask for password only one time. Has anyone already written a such script?
1
6027
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 required fields in a form have values. I'm writing the values to the client using document.write only so that I can confirm that the values are there to be played with - this is not the final result so please (unless it's leading to the script failure)...
6
2519
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 automatically calculate how much time is inbetween. That part of it is working fine, but what ISN'T working fine is the script that validates whether or not the user has entered the time in proper syntax, and to make sure that the Time In does not...
4
8753
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 then checked the Database Maintenance Plan History page, but this shows all the steps having run successfully. If I check the drives for the actual backup files, they exist and look healthy too!
8
4279
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 chooses a state and so forth for the cities too. I will need to get the data from an Oracle database. Thanks so much in advance.
5
2637
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 successful authentication, it opens the Switchboard). All it does is get the username and password, pull up a recordeset of the usernames and sets the UserId to be used throughout the session. The table Userlist is a SQL 2000 linked table. I can...
2
19061
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 occasion to experience a failure and got a code of 2??? Would like to know just what that means. Here's how I call the fn... Result = SendMail((F!Subject), (F!To), (F!CC), (F!Attach), (F!Message))
3
2616
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 system in place. It uses a web interface to allow members of staff to select their site location for any week, request leave and record absences. The server-side scripting is composed of VB/ASP and Javascript which
0
1894
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 error - MSDN suggests http://support.microsoft.com/default.aspx/kb/224370 However I can PING the share just fine ! Other clues - is this code example DOA ? or is there something else ? --
0
9639
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9474
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10308
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10076
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9939
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8964
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5507
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4040
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.