aztec Posted March 25, 2008 Share Posted March 25, 2008 Hello Using PHP and MySQL I am getting the data I need from the database. Sometimes it can be 2 rows and sometimes 17 rows. I have a need to assign a different variable to each row when I iterate through the array. Is this possible to do automatically using PHP. I have hard coded this into a test page but this is very 'messy' and you have to put in the correct number of variables and multiple 'if' statements to achieve it, plus you need to know how many rows there are so you can put in the correct number of variables, and each call is different. Kind Regards Link to comment https://forums.phpfreaks.com/topic/97832-assigning-a-variable/ Share on other sites More sharing options...
pocobueno1388 Posted March 25, 2008 Share Posted March 25, 2008 Could you explain why are are wanting to assign each row to a different variable? Maybe someone can suggest a better way. Also, it would be helpful if you posted the code you already have. Link to comment https://forums.phpfreaks.com/topic/97832-assigning-a-variable/#findComment-500534 Share on other sites More sharing options...
discomatt Posted March 25, 2008 Share Posted March 25, 2008 $q = 'SELECT * FROM `table`'; $r = mysql_query($q); while($d = mysql_fetch_assoc($r) ) $a[] = $d; print_r($a); Link to comment https://forums.phpfreaks.com/topic/97832-assigning-a-variable/#findComment-500537 Share on other sites More sharing options...
aztec Posted March 25, 2008 Author Share Posted March 25, 2008 Hello The webpage I am constructing uses the name returned and converts it into a hyperlink. So I need to have precise control over what goes where on the page $x=4; while ($result = mysql_fetch_array($results)) { $id = $results['id']; if ($x == 4) {$row1 = ($result);} if ($x == 5) {$row2 = ($result);} $x++; } echo $row1 [first_name] ." " . $row1 [surname]; ?><br /> <?php echo $row2 [first_name] . " " .$row2 [surname]; ?> Regards Link to comment https://forums.phpfreaks.com/topic/97832-assigning-a-variable/#findComment-500544 Share on other sites More sharing options...
discomatt Posted March 25, 2008 Share Posted March 25, 2008 I'm not sure what you mean. The code i provided dumps all results into an array, organized by the order the rows were returned from the query. In order to have custom results, you have to have custom code. The more complex the results, the more complex the code. If you want more detailed help, please provide a more detailed request or question. Link to comment https://forums.phpfreaks.com/topic/97832-assigning-a-variable/#findComment-500552 Share on other sites More sharing options...
aztec Posted March 25, 2008 Author Share Posted March 25, 2008 Hello discomatt Thanks for your reply. Looking at what it produces is quite complex, but it does provide me one answer. Could I ask you how would you extract say the second row, field 2. At this stage of knowledge that would help me to get to grips with extracting the data I need. Regards Link to comment https://forums.phpfreaks.com/topic/97832-assigning-a-variable/#findComment-500560 Share on other sites More sharing options...
discomatt Posted March 25, 2008 Share Posted March 25, 2008 Well, using mysql_fetch_assoc returns column names for array keys, so that would be cumbersome... An easier method would be to change it to this: $q = 'SELECT * FROM `table`'; $r = mysql_query($q); while($d = mysql_fetch_row($r) ) $a[] = $d; print_r($a); You would access rows and fields using their respective places - 1 so, 2nd row, 2nd field would be $a[1][1]; 5th row, 3rd field would be $a[4][2]; ect. Link to comment https://forums.phpfreaks.com/topic/97832-assigning-a-variable/#findComment-500571 Share on other sites More sharing options...
cooldude832 Posted March 25, 2008 Share Posted March 25, 2008 Hello discomatt Thanks for your reply. Looking at what it produces is quite complex, but it does provide me one answer. Could I ask you how would you extract say the second row, field 2. At this stage of knowledge that would help me to get to grips with extracting the data I need. Regards If you only want a specific portion of the results than your where portion of the query is either non existent or too loose Link to comment https://forums.phpfreaks.com/topic/97832-assigning-a-variable/#findComment-500576 Share on other sites More sharing options...
aztec Posted March 25, 2008 Author Share Posted March 25, 2008 Hello Discomatt Thanks a lot for your help and your code, you have moved my project forward my a considerable amount. I can now report that I can get the exact data I need and use it in the way I want. Once again thanks Kind Regards The topic is now resolved Link to comment https://forums.phpfreaks.com/topic/97832-assigning-a-variable/#findComment-500605 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.