•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 403,326 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,055 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 488 | Replies: 10 | Solved
![]() |
I use an array for the first time in a while loop without declaring it(as php does not need it)
As the while loop is repeating it increases the number of the array.
Am I doing this right?
Should I have before the loop some code like:
Dont know abit confused and lost.
Thanks, Regards X
syntax Syntax (Toggle Plain Text)
$variable_array[] = $fetch_array['column'];
As the while loop is repeating it increases the number of the array.
Am I doing this right?
Should I have before the loop some code like:
syntax Syntax (Toggle Plain Text)
$variable_array = array();
Dont know abit confused and lost.
Thanks, Regards X
Last edited by OmniX : May 9th, 2008 at 4:40 am.
•
•
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation:
Rep Power: 8
Solved Threads: 239
As you have already said it yourself, you don't have to explicitly declare a variable as an array to use it like an array. You can simply use $var[] = "value"; This will treat $var as an array. But, its always a good practice to initialize a variable before using it. (But most of them don't do it
)
) Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
*PM asking for help will be ignored*
*PM asking for help will be ignored*
•
•
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation:
Rep Power: 8
Solved Threads: 239
php Syntax (Toggle Plain Text)
<?php $var = array( ); //declare an array $query = "select * from table"; $result = mysql_query($query); while($row = mysql_fetch_array($result)) { $var[] = $row['name']; //assign a value } ?>
But, you can't use a normal variable to act like an array. Example,
php Syntax (Toggle Plain Text)
<?php $x = 100; $x[] = "10"; print $x; ?>
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
*PM asking for help will be ignored*
*PM asking for help will be ignored*
•
•
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation:
Rep Power: 8
Solved Threads: 239
Its being stupid, in short.
It wont let me declare the array before the html, I thought it was possible?
Anyways I have found another way to do it, just need a questions answered.
[code syntax="php"]
while($row = mysql_fetch_array($result)) {
//then to release after it is used so you can reuse it is?
mysql_free_result($result);
[/code]
This correct, Thanks.
It wont let me declare the array before the html, I thought it was possible?
Anyways I have found another way to do it, just need a questions answered.
[code syntax="php"]
while($row = mysql_fetch_array($result)) {
//then to release after it is used so you can reuse it is?
mysql_free_result($result);
[/code]
This correct, Thanks.
Last edited by OmniX : May 9th, 2008 at 7:25 am.
•
•
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation:
Rep Power: 8
Solved Threads: 239
When you execute a query, a resource identifier will be assigned to $result. mysql_free_result will free this value.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
*PM asking for help will be ignored*
*PM asking for help will be ignored*
Allowing me to reuse the query again or in other words fetch it again?
Man nav php can be so annoying at times I fix one error and a new error pops up. I throw the arrays in a forloop and now more errors
. Ill get back to debuging and keep you posted to see if anything works.
PS: can you run queries in a for loop? That could be a possible error? or declare it differently?
Man nav php can be so annoying at times I fix one error and a new error pops up. I throw the arrays in a forloop and now more errors
. Ill get back to debuging and keep you posted to see if anything works.PS: can you run queries in a for loop? That could be a possible error? or declare it differently?
Last edited by OmniX : May 9th, 2008 at 8:22 am.
•
•
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation:
Rep Power: 8
Solved Threads: 239
I don't know whats causing the error. You can run queries in a for loop. Once you have freed the result using mysql_free_result, that resource identifier is freed. Actually, if your query isn't too complicated, you don't have to use free_result as php frees the result at the end of execution of the script. Source: http://nl3.php.net/mysql_free_result
Can you post your code here ?
Can you post your code here ?
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
*PM asking for help will be ignored*
*PM asking for help will be ignored*
![]() |
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Functions and Array help (C++)
- Initialization of Const Arrays by function (C)
- Help : Pointers to array of class objects . (C++)
- Reading an array from a file (Java)
- template array initialization (C++)
- 'C' initializer problem (C)
- problem creating a for loop for an array (C++)
- Return multidimension array (C)
Other Threads in the PHP Forum
- Previous Thread: public/private in php
- Next Thread: Search from multiple tables ??



Linear Mode