laura29 Posted June 12, 2008 Share Posted June 12, 2008 hello, I have 10 rows in a table in database Mysql. Now I need put field 1 of all 10 rows in a array but I don't know how to put in. I really appreciate if you can assistance me. thanks --------------------------------------------------------------------- This is code but it does not run <? $sqlautolink="select * from linkauto"; $result_autolink=mysql_query($sqlautolink) or die("Not Query linkauto"); $link = array(); while($rowslinkauto=mysql_fetch_row($result_autolink)) { $link[] = $rowslinkauto["link"]; } $j=0; for ($j=0; $j<=strlen($link); ++$j) {echo $link[$j]."<br>"; } ?> Quote Link to comment Share on other sites More sharing options...
bluejay002 Posted June 12, 2008 Share Posted June 12, 2008 <? $sqlautolink="select * from linkauto"; $result_autolink=mysql_query($sqlautolink) or die("Not Query linkauto"); $link = array(); while($rowslinkauto=mysql_fetch_row($result_autolink)) { $link[] = $rowslinkauto; } print_r($link); ?> try this instead. then post back what is displayed... just replace with your running code. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted June 12, 2008 Share Posted June 12, 2008 As i understood the the question, only one field from the datatabase was required. Using bluejay002's code, you would have a multidimensional array with all the fields. The problem with your code, laura, is that you are using strlen() to see how many elements there are in the array; you should be using count: $j=0; for ($j=0; $j<=count($link); ++$j){ echo $link[$j]."\n"; } You might find it easier to use a foreach loop with arrays, however: foreach($link as $v){ echo $v."\n"; } Finally, if you did only want the one field from your table, its very inefficient to select all the rows as you are doing. Modify your query to: select link from linkauto P.S. Welcome to the forums Quote Link to comment Share on other sites More sharing options...
bluejay002 Posted June 12, 2008 Share Posted June 12, 2008 yeah... i was assuming that she might get all the data instead, given the query resource has several columns. print_r() was really my thing there so that she can check her array if its empty or not. quick find there! i haven't seen that one. Quote Link to comment Share on other sites More sharing options...
conker87 Posted June 12, 2008 Share Posted June 12, 2008 Wouldn't it be easier to us mysql_fetch_array() if you want your values in an array? Quote Link to comment Share on other sites More sharing options...
bluejay002 Posted June 12, 2008 Share Posted June 12, 2008 Wouldn't it be easier to us mysql_fetch_array() if you want your values in an array? yeah, its in array form but it only returns one row at a time. also, you will have trouble with that going to certain rows and going back to previous rows. what the code below does is that it makes the resource into array, multidimensional array that is: first index specifying the row, second index specifying the column. so you have all the rows and columns from your resource into an array and you can use all array related functions here which might come handy. Quote Link to comment Share on other sites More sharing options...
conker87 Posted June 12, 2008 Share Posted June 12, 2008 Hmm! That's quite handy indeed. Quote Link to comment Share on other sites More sharing options...
laura29 Posted June 13, 2008 Author Share Posted June 13, 2008 Thanks for all but those reply is not what I had in mind This is table linkauto id link link_url 1 newyork museum http://www.abc.com/newyork_museum.html 3 newyork http://www.abc.com/newyork.html 4 newyork hotel http://www.abc.com/newyork_hotel.html 5 newyork tour http://www.abc.com/newyork_tour.html I need a query to get auto field link as $link= Array('newyork museum','newyork','newyork hotel','newyork tour') Quote Link to comment Share on other sites More sharing options...
bluejay002 Posted June 13, 2008 Share Posted June 13, 2008 well if that's the case, then take GingerRobot's suggestion... instead of using strlen(), use count(). 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.