Question : Queries inside a while loop

Hello Experts.  I need your help!!!

I'm trying to figure out a way to validate certain field values in each row of a MySQL table using PHP code.  In the example below, I am using data from two databases located on two different servers.  Everything works perfectly until I try to validate a field value in the first table by comparing it to a list of values in the second table.  As soon as the WHILE loop encounters the code in the IF statement below, it shuts down.  If I remove the IF statement and thus, any reference to the validation function it calls, the loop works perfectly.

Is there a way to get around this problem or can I not run another MySQL query inside the loop?  Some example code would be very helpful.

Thanks,

SPF  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
##	STEP 3 - Validate the contents of each row
    $sql	= "SELECT * FROM ihcsecure.csv_import";
    $result	= mysql_query($sql,$link1) or die(mysql_error());
    $rcount	= mysql_num_rows($result);
    echo '<br/><br/>There are now '.$rcount.' rows of data in the csv_import table.<br/>';    
    
    $i = 0;
    while ($records = mysql_fetch_array($result)) {
    	$i = $i+1;
    	$dmpo_id = $records['group_id'];
    	$type 	 = $records['type']; 
    	echo '<br/>The DMPO ID for row '.$i.' is '.$dmpo_id;
    	if (isValid($type) > 0){
    		echo 'The type code is valid';
    	}else {
    		echo 'The type code is not valid';
    	}    	    	
    }
    
    function isValid($cType){
    	$sqlx = "SELECT * FROM carecards.nbi_types where type = '$cType'";
    	$types = mysql_query($sqlx,$link2) or die(mysql_error());
    	$vType = mysql_num_rows($types);
    	return $vtype;
    }

Answer : Queries inside a while loop

That's normal behavior for a maintenance plan backup.

It's better to write your own SQL commands for backup and run them with a scheduled job.
Random Solutions  
 
programming4us programming4us