...

View Full Version : while doesn't work



devil_online
02-14-2006, 03:45 AM
Hi,
I have a code that uses the While and it insert data to the datatable
I want that while there is any name equal to that it wont insert any more data.

However I can't make it work

here is my code:


$resource = mysql_num_rows($sql);
while ($resource <= 1){

insertname();
}


I want to use while to loop through things untill a condition changes.
How can I make it work?

_Aerospace_Eng_
02-14-2006, 04:58 AM
Try echoing resource to see what value it gets.
$resource also needs to be incremented some how.
It should probably be something like


$resource = mysql_num_rows($sql);
$i = 0;
while ($i <= $resource){
insertname();
$i++;
}

Bio2hazard
02-14-2006, 09:20 AM
Could it be that the amount of rows that get returned from the mysql_num_rows are higher than 1 ?

If the condition of while isn't matched at the beginning, it gets skipped which would explain why it does not get executed.

If you want it to loop trough the while at least once, you could use the do-while loop.

I think the syntax was
do {
code..
} while (condition );

But yes, as _Aerospace_Eng_ said, you should echo it for debugging and see if everything goes the way you want; also you do need some kind of increment in the loop else chances are it turns to an endless loop :)

- Felix

devil_online
02-14-2006, 09:50 PM
I think the problem is with the funtion
here is the complet code


function insertname(){
global $link;
$firstnames = array("Jonh", "Paul", "Bill");
$lastnames = array("Simon", "Luck","Guy");
$name = $firstnames[array_rand($firstnames)]." ".$lastnames[array_rand($lastnames)];
$sql = mysql_query("SELECT names from home
WHERE names = '$name'
LIMIT 1") or die(mysql_error());
$resource = mysql_num_rows($sql);
print $name;
$query = "INSERT INTO home(names)
values('$name')";
mysql_query ( $query, $link) ;

}


while ($resource <= 1){

insertname();
}

I don't want to increase the resourse. I want that the condition says while the resourse is not 0 it trys to create a new name

thanks

_Aerospace_Eng_
02-15-2006, 12:54 AM
Did you even echo $resource to see what its returning? It could be that $resource is already greater than 1 so the while loop will never occur.

devil_online
02-15-2006, 03:39 AM
no
the resource is 0

I think is because I've put the resourse inside the funtion.

What do you think

thanks

Bio2hazard
02-15-2006, 04:34 AM
No variables are transfered between functions unless you tell them to be. ( or they are global )
..
In this case resource doesn't seem to be global so try this:


function insertname(){
global $link;
$firstnames = array("Jonh", "Paul", "Bill");
$lastnames = array("Simon", "Luck","Guy");
$name = $firstnames[array_rand($firstnames)]." ".$lastnames[array_rand($lastnames)];
$sql = mysql_query("SELECT names from home
WHERE names = '$name'
LIMIT 1") or die(mysql_error());
$resource = mysql_num_rows($sql);
print $name;
$query = "INSERT INTO home(names)
values('$name')";
mysql_query ( $query, $link) ;
return ($resource);
}

$resource = insertname();
while ($resource <= 1){

insertname();
}

Although I am not sure if that works too well...

_Aerospace_Eng_
02-15-2006, 05:55 AM
Try this

$resource = mysql_num_rows($sql);
$i = 0;
function insertname(){
global $link;
$firstnames = array("Jonh", "Paul", "Bill");
$lastnames = array("Simon", "Luck","Guy");
$name = $firstnames[array_rand($firstnames)]." ".$lastnames[array_rand($lastnames)];
$sql = mysql_query("SELECT names from home
WHERE names = '$name'
LIMIT 1") or die(mysql_error());
print $name;
$query = "INSERT INTO home(names)
values('$name')";
mysql_query ( $query, $link) ;
}
while ($i <= $resource){

insertname();
$i++;
}
If resource is returning 0 then the loop will run one time. I don't think you want an infinite loop.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum