SunQuest
           PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old May 31st, 2002, 09:23 PM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
Array($variable)

I am trying to use an array filled with information taken from a mySQL database. When I try to use the array I get an error.

Variable equals 2,3,4,5
Which will be retrieved from the database prior to the array.

array ($variable)

Am i missing something. Can you not dump a variable into an array()?

Reply With Quote
  #2  
Old May 31st, 2002, 09:25 PM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
RE: Array($variable)

What error are you getting?

Reply With Quote
  #3  
Old May 31st, 2002, 09:55 PM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
RE: Array($variable)

I am trying to use the array in a foreach statement.

$array = array($variable);
foreach( $array as $id)
{
// Output
}

I get Invalid argument supplied foreach.

Reply With Quote
  #4  
Old May 31st, 2002, 09:56 PM
notepad notepad is offline
Codewalkers Loyal (3000 - 3499 posts)
 
Join Date: Apr 2007
Location: Central, IL USA
Posts: 3,214 notepad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Send a message via AIM to notepad
RE: Array($variable)

try it like this

$myArray[] = "$variable";

Reply With Quote
  #5  
Old May 31st, 2002, 10:04 PM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
RE: Array($variable)

will that work for a series of numbers?

2,3,4,5

Reply With Quote
  #6  
Old May 31st, 2002, 10:13 PM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
RE: Array($variable)

This is how i am loking for it to work.

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT variable1,variable2,variable3 FROM sometable";
$result=mysql_query($query);
mysql_close();

$num=mysql_numrows($result);
$i=0;
while ($i < $num) {

$Variable1=mysql_result($result,$i,"Variable1");
$Variable2=mysql_result($result,$i,"Variable2");
$Variable3=mysql_result($result,$i,"Variable3");
++$i;
}

function makeList() {

foreach ( array($Variable1) as $id)

{

// My Function here.

}

Variable1 will be pulled from the database as a CHAR value equal to 2,3,4,5

Reply With Quote
  #7  
Old May 31st, 2002, 10:24 PM
notepad notepad is offline
Codewalkers Loyal (3000 - 3499 posts)
 
Join Date: Apr 2007
Location: Central, IL USA
Posts: 3,214 notepad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Send a message via AIM to notepad
RE: Array($variable)

i'm pretty rusty when it comes to MySQL, a nice little refresh on arrays however can be found below

http://www.devshed.com/Server_Side/...anip/page1.html

Reply With Quote
  #8  
Old May 31st, 2002, 10:24 PM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
RE: Array($variable)

So the end result will be


foreach ( array(2,3,4,5) as $id)

I need the array to contact a variable because the array values are updated rapidly.

Reply With Quote
  #9  
Old June 1st, 2002, 05:30 AM
Zoombie Zoombie is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Boston, MA, US
Posts: 19 Zoombie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to Zoombie Send a message via AIM to Zoombie
RE: Array($variable)

First off, you're calling mysql_close before you retrieve the data. You can't use any of the MySQL functions after a connection is closed. If you call a MySQL function when a connection isn't open, it tries to connect to localhost anonymously.
Secondly, I'm not exactly sure what you're trying to do. Are you trying to display the values for each record in a table for those three fields?

Reply With Quote
  #10  
Old June 1st, 2002, 06:17 AM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
RE: Array($variable)

I can echo the variables that I need, so I don't think that the mysql_close call is the problem.

The function which I left out uses the values in the initial array to retrieve and print out additional information that corresponds to the initial array items.

The initial array contains numbers which are primary keys from another table. I will pull fields from the other table using the primary keys in the initial array.

This is the function i left out

$db=mysql_connect($servername,$dbusername,$dbpassw ord);
mysql_select_db($dbname);
$action2 = mysql_query("SELECT
field1,field2,field3
FROM someothertable
WHERE userid='$id'");
while (list(
$field1,$field2,$field3
) = mysql_fetch_row($action2))
{
$print_field1 = $field1;
$print_field2 = $field2;
$print_field3 = $field3;
}

I then use the variables in my output. Which is table with a new row for each of the items in the initial array.

Reply With Quote
  #11  
Old June 2nd, 2002, 05:43 AM
Zoombie Zoombie is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Boston, MA, US
Posts: 19 Zoombie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to Zoombie Send a message via AIM to Zoombie
RE: Array($variable)

It would be much easier to help you with this problem if you gave us a little more context...trying to guess everything before, after, and even some of the inside of your excerpt isn't easy...

Reply With Quote
  #12  
Old June 2nd, 2002, 11:30 AM
dwd dwd is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: London, UK
Posts: 13 dwd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: Array($variable)

It sounds to me as if the base case for the problem could be written:

php Code:
Original - php Code
  1.  
  2. $str = '1,2,3,4,5';
  3. $arr = array( $str );


In this case, $arr will be an array of size 1, with all its elements unset, *or* an array of size 1 with a single value of $str, with the key of 0. I haven't really looked at which, although I suspect the latter.

This is not what you want... You want, I think:

php Code:
Original - php Code
  1.  
  2. $str = '1,2,3,4,5';
  3. $arr2 = explode( ',', $str );


This gives you $arr2 as an array of 5 elements:

0 => "1",
1 => "2",
2 => "3",
3 => "4",
4 => "5"

I think that's what you were intending.

You might find that your SQL database could benefit from a schema redesign, though.

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > Array($variable)


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway