0

Am making a really simple php page which pulls data from a really simple database and displays it as charts for a wall monitor.

I've got the data from the db, no problem, but I seem to be struggling splitting that data up

For now i'm just echoing the data to make sure i have what i need.

My code looks like this:

    <?php 
     mysql_connect("host", "user", 'password' or die(mysql_error()); 
     mysql_select_db("databax") or die(mysql_error()); 
     $dbdata = mysql_query("SELECT * FROM dbcpman_resources") 
     or die(mysql_error()); 
     $column = mysql_fetch_array( $dbdata );
     echo $column[0]."<br>";
     echo $column[1]."<br>";
     echo $column[2]."<br>";
     echo $column[3]."<br>";
     echo $column[4]."<br>";
     echo $column[5]."<br>";
     ?> 

Indeed, it works - it will echo data from the database, but as i've not specified the row anywhere, its just giving me the first row.

I need to be able to work with each row seperately. There will only ever be 6 rows in this table.

So can anyone help me out with how I go about replicating this for rows 2,3,4,5 and 6?

Thanks in advance!! :)

2

3 Answers 3

1

You need to loop over the result set. The easiest way is to use a while loop as this automatically terminates at the end of the result set, like this.

while ( $row = mysql_fetch_array( $dbdata );
   echo $row [0]."<br>";
   echo $row [1]."<br>";
   echo $row [2]."<br>";
   echo $row [3]."<br>";
   echo $row [4]."<br>";
   echo $row [5]."<br>";
}

Also if you were to change the function that returns the resuilts to use mysql_fetch_assoc() you can reference each field with the name it has on the database so the code is easier to read, like this:

I dont know your field names so I made some up.

while ( $row = mysql_fetch_array( $dbdata );
   echo $row ['name']."<br>";
   echo $row ['date']."<br>";
   echo $row ['time']."<br>";
   echo $row ['value1']."<br>";
   echo $row ['value2']."<br>";
   echo $row ['value3']."<br>";
}
Sign up to request clarification or add additional context in comments.

Comments

1

First of all, I'd rather use mysql_fetch_assoc() instead of mysql_fetch_array() since it doesn't srew up your result, if the table structure changes. It would be even better if you used either mysqli or PDO instead of mysql_* functions, since they are marked deprecated already!

Second please note, that either function just fetches ONE record from your resultset at a time. To fetch all records try the following:

$records = array();
while($row = mysql_fetch_assoc($dbdata)) {
  $records[] = $row;
}

You can do a print_r($records); to see what's inside $records after fetching all.

Comments

0

put this line $column = mysql_fetch_array( $dbdata ); in while loop like this

while($column = mysql_fetch_array( $dbdata ))
{
echo $column[0]."<br>";
 echo $column[1]."<br>";
 echo $column[2]."<br>";
 echo $column[3]."<br>";
 echo $column[4]."<br>";
 echo $column[5]."<br>";

}

3 Comments

Would i need to use an if statement to assign value to specific variables? the columns in my db are : type, capacitytotal, capacityused, percentused. My end goal is to be able to use these values with google charts like so... var data = google.visualization.arrayToDataTable([ ['Resource', 'capacity'], ['Used', <?php echo $memoryused ?>], ['Available', <?php echo $memoryavailable ?>], etc
sorry i dont have ideas about google charts,also i cant get your question Would i need to use an if statement to assign value to specific variables?
nevermind, you're response is ultimately the correct answer, i just need to wake my brain up a little more :) Thank you

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.