Jump to content

Explode file to array and use parts for MySQL?


pakenney38

Recommended Posts

I really don't know how to explain this, but I have a Tab-delimited file that I want to read line-by-line. In a line is an employee number. I want to get the lastname and firstname of the employee from a MySQL table using the employee number as the relative key. I want to do this for each line then display the results on the screen.

 

The problem is that the script gets the firstname and lastname of the employees until it hits an employee that is not in the MySQL table, then it fails to get the firstname and lastname for the remainder of the employees, even if some of them are in the MySQL table.

 

Here is my example:

<?PHP
$host = "XXX";
$user = "XXX";
$pwd = "XXX";

$filename = "C:\aaron.csv";
$fh = fopen($filename, 'r');
$theData = fread($fh, filesize($filename));
fclose($fh);

$line = explode("\r\n", $theData);

mysql_connect($host, $user, $pwd);
mysql_select_db("database") or die("Error: " . mysql_error());

foreach ($line as $key => $value)
{
$info = explode("\t", $value);
$info[1] = trim($info[1]);
$empno = $info[1];

echo $info [1];
echo "<br>";
echo $empno;
echo "<br>";

$query = "SELECT lastname, firstname FROM name WHERE empnumber = '$empno'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$lastname = $row['lastname'];
$firstname = $row['firstname'];
echo $row['lastname'];
echo "<br>";
echo $lastname;
echo "<br>";
echo $row['firstname'];
echo "<br>";
echo $firstname;
echo "<br><br>";
}
mysql_close();
?>

 

Any help would be great thank you. This is driving me nuts.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.