I use an array for the first time in a while loop without declaring it(as php does not need it)

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

$variable_array = array();

Dont know abit confused and lost.

Thanks, Regards X

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

Nav to the rescue again!

Umm nav I can declare it but I cant initialize it as I dont want to give it a value till used in the while loop, so.

What code would I use?

$var[];

?

<?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 
}
?>

Note: The code works even without declaring a variable as array.
But, you can't use a normal variable to act like an array. Example,

<?php
$x = 100;
$x[] = "10";
print $x;
?>

Ya nav that what I have, but I cant seem to print the $var variable should just be print/print_r/echo $var; but none of it works...

Why ? print_r($arrayname) should work fine.

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.

while($row = mysql_fetch_array($result)) {
//then to release after it is used so you can reuse it is?
mysql_free_result($result);

This correct, Thanks.

When you execute a query, a resource identifier will be assigned to $result. mysql_free_result will free this value.

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?

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 ?

I cannot reuse the query after I have mysql_free_result it.

I really thought you could reuse a query if required. Just had to refresh it after use or something, know anything about this?

If not Ill just have to make a duplicate query if required.

Thanks for your help nav helped me to solve the other issue I had.

Regards X

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.