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. -
<?
-
$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 -
'; 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
7 2120
If you get this type of output to the browser, try to put <?php instead of <?
I get an empty screen with this, what have I missed -
<?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. -
<?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.
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.
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. -
<?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 />";
-
}
-
-
-
?>
-
@Atli Atli 5,058
Expert 4TB
That would probably be because your strings are not quoted. -
// 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=\"whatever\">{$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;
-
You could probably avoid all these basic syntax errors if you read through a good PHP tutorial real quick. Like say, this one.
Many thanks for the advice. Below is the resulting 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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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....
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
| |