Jump to content


Photo

Create an Array From a MySql Field (or Column)


  • Please log in to reply
5 replies to this topic

#1 drknife

drknife
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 30 July 2006 - 04:10 PM

I have been trying to make something do this for hours.  I have scoured the net looking for the question previously asked.
I found nothing that worked.  I found this not very useful posting where the poster said they fixed the error I keep having but did not post the fix.

http://codewalkers.c...3&realm=default

so I ask with all humbleness and desparation:

How do I take the data from one field in a table in a mysql database and create a php array with that data?
Or the real goal is to select a random entry from the pagelist field to load a random webpage?
I have the following code in use

<?php

//connect to database
$dbh = mysql_connect ("localhost", "user", "pass") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db("dbname");

$sql="SELECT pagelist FROM pagelist";
$result = mysql_query($sql) or die(mysql_error());

$input = mysql_fetch_array($result);

while($row = mysql_fetch_array($result)){
//echo $row['pagelist'];
//echo "<br />";
}
echo $row[0] ;
echo $row[1] ;
echo $row[2] ;

//random page
//$input = array("index.html", "index2.html", "gwbush.html");
$page = array_rand($input);
echo $input[$page] . "\n";


?>


#2 Drumminxx

Drumminxx
  • Members
  • PipPipPip
  • Advanced Member
  • 92 posts
  • LocationUSA

Posted 30 July 2006 - 04:22 PM

are you saying that you want to create an array from a string that contains 'index.html, page.html, page2.html'?

you could use the explode function to create an array, assuming that the seperator is a comma it would go something like this

$myarray = explode(",", $input);

:: gucci.com ::
:: blockbuster.com ::

--------------------

#3 drknife

drknife
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 30 July 2006 - 04:29 PM

I want to take the pagelist field from my mysql database an create an array from that  OR chose a random row from the pagelist field/column.

The above code resulted in the following error


Parse error: syntax error, unexpected T_VARIABLE in host/random.php on line 6


#4 para11ax

para11ax
  • Members
  • PipPip
  • Member
  • 21 posts
  • LocationColumbus, OH

Posted 30 July 2006 - 04:31 PM

Here's some code I cooked up, it's probably not the most efficient, but it should do it:

//Select URLs from database?
$sql_pages = mysql_query("SELECT pagelist FROM pagelist") or die(mysql_error());
$num_rows = mysql_num_rows($sql_pages);

//Add all pagelists to array
$count = 0;
while($count < $num_rows){
  $row = mysql_fetch_array($sql_pages);
  $page_array[$count] = $row['pagelist'];
  $count++;
}

//Select a random pagelist entry
$rand_sel = rand(0, $num_rows);
$rand_page = $page_array[$rand_sel];

I may have misinterpreted the question.  Is pagelist a list of pages for each entry in this table and you only want to compile an array for a single entries pagelist?  Or, are you trying to put all "pagelists" into an array (putting a database column into an array) - that is what the above code should do.  Disregard it if you are trying to do the former.

#5 Drumminxx

Drumminxx
  • Members
  • PipPipPip
  • Advanced Member
  • 92 posts
  • LocationUSA

Posted 30 July 2006 - 04:32 PM

ok try it like this, based on your code:

$input = mysql_fetch_array($result);
$myarray = explode(",", $input[0]);
:: gucci.com ::
:: blockbuster.com ::

--------------------

#6 drknife

drknife
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 30 July 2006 - 04:39 PM

para11ax:

Thanks that did what I wanted. 
The other code helped to for me to see how to create an array a different way.

Now I will try to get this code streamlined.

Thanks.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users