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 Quote Link to comment 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. Quote Link to comment 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); Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.