Jump to content

How do I turn a SQL query into variables?


yuckysocks

Recommended Posts

Hello. I have a query, and I want the results eventually to be variables that I serve to a JS library to make graphs. I have this currently:

$result = mysql_query("SELECT * FROM weatherdata ORDER BY id DESC LIMIT 10");
$row = mysql_fetch_array($result) or die(mysql_error());
$row2 = array_reverse($row);

foreach($row2 as $a){
echo $a."<br />";
}

 

The table is 5 fields (including the key ID).  The returned output is only the most recent entry, twice!

18<br />18<br />
13<br />13<br />
51<br />51<br />
Mostly Cloudy<br />Mostly Cloudy<br />
Last Updated on Mar 14, 4:54 pm EDT<br />Last Updated on Mar 14, 4:54 pm EDT<br />

 

With the actual DB entry looking like this:

  • ID = 18
  • Wind = 13
  • temp = 51
  • weatherstr = Mostly Cloudy
  • date = Last updated on Mar 14, 4:54 pm EDT

 

I don't know why it only shows the most recent entry (DB has >50 entires so far, it's populated hourly), nor why it's shown twice.

 

What I'm looking for, eventually, is to query the 10 most recent entries, and set the "temp" result for each as a variable ($a, $b, $c...) and then feed those variables to a JS library to make a graph.

 

 

Any suggestions would be helpful!

Link to comment
Share on other sites

you are only fetching once thus getting one results!

its showing twice because your using fetch_array that has numeric and assocated

try a loop ie

<?php
$result = mysql_query("SELECT * FROM weatherdata ORDER BY id DESC LIMIT 10");
while($row = mysql_fetch_array($result))
{
   echo $row['ID']." - ".$row['Wind ']."<br />"; //etc
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.