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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.