John
John

Reputation: 785

Getting specified rows from an array

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!! :)

Upvotes: 0

Views: 81

Answers (3)

Anticom
Anticom

Reputation: 1013

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.

Upvotes: 1

RiggsFolly
RiggsFolly

Reputation: 94662

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>";
}

Upvotes: 1

krishna
krishna

Reputation: 4099

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>";

}

Upvotes: 0

Related Questions