Jump to content

CREATE ARRAY FROM DATABSE MySQL


laura29

Recommended Posts

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>"; }

 

?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/109853-create-array-from-databse-mysql/
Share on other sites

<?
$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. :)

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 :)

 

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.

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')

 

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.