If I use the line:
while( $rows[]=mysql_fetch_array($result) );
How do I retreive my data without starting a loop? Let's say I want the FIELD CAR_ID from the first row of results and the FIELD CAR_ID from the second row of results, followed by the FIELD CAR_NAME from the first row and FIELD CAR_NAME from the second row. Is there a way to refer to this elements of the array INDIVIDUALLY, without looping through each row, one at a time with WHILE?
OK...let's see if this makes it clearer...the code below will retreive DAN'S records and use WHILE to loop through them, one row at a time.
$sql= "SELECT * FROM TABLE WHERE USER = 'DAN'";
$result = @($sql,$connection) or die ("Couldn't select database. $sql");
while($row= mysql_fetch_array($result, 1)) {
$USER = $row[USER];
$CAR_ID = $row[CAR_ID];
$CAR_NAME = $row[CAR_NAME];
}
INSTEAD OF THIS, I want to get the results and NOT LOOP through them with WHILE.
After these two lines it is my understanding that $result holds the data returned from my query:
$sql= "SELECT * FROM TABLE WHERE USER = 'DAN'";
$result = @($sql,$connection) or die ("Couldn't select database. $sql");
I wish to use the data in $result later in the page to drop data into an HTML table.
Is there any way to do this WITHOUT using WHILE.
I found the following code which SEEMS to do something like what I am looking for, but I am not sure -- it's a bit too dense for me. Or, rather I am a bit too dense for it. I keep reading through it but the way the loops interact evade my logic.
The below is claimed to:
Build an array of associative arrays that can be accessed by $ARRAY[$index]["DB field name"]. Very handy for pulling select fields from a large table.
<?php
$result_num = mysql_list_fields($dbName,$tab);
$count = mysql_num_fields($result_num);
//build array of column names
for($j = 0; $j < $count; $j++){
$name = mysql_field_name($result_num,$j);
$fieldArr[$j] = $name;
}
$result_num1 = mysql_query($qStr);
if($result_num1){
$numRowsFound = mysql_num_rows($result_num1);
}
else {
//ERROR CONDITION
}
for($i = 0,$result = mysql_fetch_row($result_num1);$i < $numRowsFound;$result = mysql_fetch_row($result_num1),$i++) {
for($j = 0; $j < $count; $j++){
$memArr[$i][$fieldArr[$j]] = $result[$j];
}
}
?>
Thank you,
Kev