|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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()? |
|
#2
|
|||
|
|||
|
RE: Array($variable)
What error are you getting?
|
|
#3
|
|||
|
|||
|
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. |
|
#4
|
|||
|
|||
|
RE: Array($variable)
try it like this
$myArray[] = "$variable"; |
|
#5
|
|||
|
|||
|
RE: Array($variable)
will that work for a series of numbers?
2,3,4,5 |
|
#6
|
|||
|
|||
|
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 |
|
#7
|
|||
|
|||
|
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 |
|
#8
|
|||
|
|||
|
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. |
|
#9
|
|||
|
|||
|
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? |
|
#10
|
|||
|
|||
|
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. |
|
#11
|
|||
|
|||
|
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...
|
|
#12
|
|||
|
|||
|
RE: Array($variable)
It sounds to me as if the base case for the problem could be written:
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: 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. |
![]() |
| Viewing: Codewalkers Forums > PHP Related > PHP Coding > Array($variable) |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|
|
|