PDA

View Full Version : What is the Query to get all the rows into an array


2bz2p
05-21-2008, 06:54 PM
Hi,

So the title basically asks the question, though this is a more detailed question:

I have a table with 4 columns and 4 rows of info
Table: Ranks
1st column: id
2: rank
3: Min_hours
4: max_hours

Now I can accomplish what I want to do with the info, so thats done.

But when I query the Db it puts 1 row into the array. What I want is all of the rows into an array. So what is the command to obtain everything into the array.

Since this whole section is going to be end user driving (in the sense they input the ranks and requirements) I have to use variables for the values vs using the id or idividual values.

Thanks
2b

davidj
05-21-2008, 08:19 PM
need to see your code

2bz2p
05-21-2008, 09:21 PM
There is alot of trial and error code in this but I am sure you will get the idea of what I am trying to do.

But basically I am trying to get the if statement to run through each row until it finds the correct rank and then just echo the appropriate rank.

I did get it to work with the first row and it does echo the correct rank, though when I increase the hours past 50 it shows a blank page, then when I do the print_r I find out that the query is building an array for just the first row. Hence its not going to find the others.

The reason for the two queries is just part of the trial and error.


heres the table
id rank min hours max hours
1 co pilot 0 50
2 pilot 50 100
3 good pilot 100 300

<?php
mysql_select_db($database_conn_abrv, $conn_abrv);
$query_ranks = "SELECT * FROM ranks";
$ranks= mysql_query($query_ranks, $conn_abrv) or die(mysql_error());
$row_ranks = @mysql_fetch_array($ranks);
/*$row_ranks = mysql_fetch_assoc($ranks);
$total_rows_ranks = mysql_num_rows($ranks);*/

/*echo "<pre>" ;
print_r ($total_rows_rank) ;
echo "</pre>" ;*/


/////////////////////////////////////////////////////////////////////////////////////////////////
$query = sprintf ("SELECT * FROM ranks");
$results = @mysql_query($query);
$row_account = @mysql_fetch_array($results);
/////////////////////////////////////////////////////////////////////////////////////////////////

echo "<pre>" ;
print_r ($row_account) ;
echo "</pre>" ;



print_r ($row_ranks) ;

$hours = 5;

do {


if ($hours >= $total_rows_rank['min_hours'] and $hours < $total_rows_rank['max_hours'])
{
$ranker = $total_rows_rank['ranks'];
echo $ranker;
}
/*}
while ($rows_rank = mysql_fetch_assoc($rank));

?>

lux
05-22-2008, 09:30 AM
example of how to return rows from an array of values retrieved from a database query:


<?php
mysql_select_db($database_conn_abrv, $conn_abrv);


//query the database
$query = sprintf ("SELECT * FROM ranks");
$results = @mysql_query($query);


//fetch results of query, place them in the array $rows and loop through each tuple/row of the $rows array
while($rows = @mysql_fetch_assoc($results)){
echo $rows['id']."<br />";
}
?>



the video tutorials are very good at explaining how it all works, so it would be useful to watch those.

also double check your if statement and make sure your using the correct operators.

let me know if you need any more info/help

2bz2p
05-22-2008, 01:40 PM
Thank you so much not only did you teach me something new, you help me accomplish what I needed to accomplish.

Below is the whole code. This is an include, so yall know.

Thanks again,
2b


/*Left here for testing purposes, and I dont have to rewrite it everytime :-)*/
/*include('../settings.php');
$hours = 590;*/

/*Query the DB*/
////////////////////////////////////////////////////////////////////////
$query = sprintf ("SELECT * FROM ranks") ;
$results = @mysql_query($query) ;
////////////////////////////////////////////////////////////////////////

/*The while runs through all of the rows with in the table until the if statment is true*/
while ($rows = @mysql_fetch_assoc($results)) {
if ($hours >= $rows['min_hours'] and $hours < $rows['max_hours'])
{
$ranker = $rows['ranks'];
echo $ranker;
}
}