I am reading a text file and processing some records, a relevant sample of the text file is
#export_dategenre_idapplication_idis_primary
#primaryKey:genre_idapplication_id
#dbTypes:BIGINTINTEGERINTEGERBOOLEAN
#exportMode:FULL
127667880285760063715151750
127667880285760123715151751
I want to perform a specific action when application_id is already stored within my database AND is_primary = 1
I wrote this PHP to test my code:
$fp1 = fopen('genre_application','r');
if (!$fp) {echo 'ERROR: Unable to open file.'; exit;}
while (!feof($fp1)) {
$line = stream_get_line($fp1,128,$eoldelimiter); //use 2048 if very long lines
if ($line[0] === '#') continue; //Skip lines that start with #
$field = explode ($delimiter, $line);
list($export_date, $genre_id, $application_id, $is_primary ) = explode($delimiter, $line);
// does application_id exist?
$application_id = mysql_real_escape_string($application_id);
$query = "SELECT * FROM jos_mt_links WHERE link_id='$application_id';";
$res = mysql_query($query);
if (mysql_num_rows($res) > 0 ) {
echo $application_id . "application id has genre_id" . $genre_id . "with primary of " . $is_primary. "\n";
} else
{
// no, application_id doesn't exist
}
} //close reading of genre_application file
fclose($fp1);
which results in this output on screen and is exactly as I expected.
371515175application id has genre_id6006with primary of 0
371515175application id has genre_id6012with primary of 1
If I then add an IF statement as in the code below, it somehow changes the value of is_primary as shown by the screen display
$fp1 = fopen('genre_application','r');
if (!$fp) {echo 'ERROR: Unable to open file.'; exit;}
while (!feof($fp1)) {
$line = stream_get_line($fp1,128,$eoldelimiter); //use 2048 if very long lines
if ($line[0] === '#') continue; //Skip lines that start with #
$field = explode ($delimiter, $line);
list($export_date, $genre_id, $application_id, $is_primary ) = explode($delimiter, $line);
// does application_id exist?
$application_id = mysql_real_escape_string($application_id);
$query = "SELECT * FROM jos_mt_links WHERE link_id='$application_id';";
$res = mysql_query($query);
if (mysql_num_rows($res) > 0 ) {
if ($is_primary = '1') echo $application_id . "application id has genre_id" . $genre_id . "with primary of " . $is_primary. "\n";
} else
{
// no, application_id doesn't exist
}
} //close reading of genre_application file
fclose($fp1);
?>
The code above results in the following screen display, which incorrectly has the first field with a primary of 1, when as can be seen by the previous screen display and the sample text file it should be 0
371515175application id has genre_id6006with primary of 1
371515175application id has genre_id6012with primary of 1
Can anyone explain what I am doing to make the variable change and how I should use the If correctly please?