I did a php script to read from text file names.txt and to do loop inside it with mysql querying to output file sex.txt which contains the names with the sex infront of it.
but i had duplication inside the output file.
can you help me here plz.
SQL statment
Code: Select all
# phpMyAdmin MySQL-Dump
# version 2.2.3
# http://phpwizard.net/phpMyAdmin/
# http://phpmyadmin.sourceforge.net/ (download page)
#
# Host: localhost
# Generation Time: Aug 08, 2006 at 05:12 PM
# Server version: 3.23.47
# PHP Version: 4.1.1
# Database : `names`
# --------------------------------------------------------
#
# Table structure for table `list`
#
CREATE TABLE list (
name varchar(10) NOT NULL default '',
sex varchar(6) NOT NULL default ''
) TYPE=MyISAM;
#
# Dumping data for table `list`
#
INSERT INTO list VALUES ('john', 'male');
INSERT INTO list VALUES ('kate', 'female');
INSERT INTO list VALUES ('mary', 'female');
INSERT INTO list VALUES ('edward', 'male');
INSERT INTO list VALUES ('shanter', 'male');
INSERT INTO list VALUES ('tony', 'male');
INSERT INTO list VALUES ('meg', 'female');
INSERT INTO list VALUES ('mike', 'male');
INSERT INTO list VALUES ('sarah', 'female');Code: Select all
<?php
$host = "localhost";
// Database Host (usually localhost)
$user = "root";
// Username
$pass = "";
// Password
$database = "names";
// Database name
?>Code: Select all
<?php
// calling the external file (has variables for the database connection)
include("db.php");
// Connect at the database using the config.inc.php
$connection = @mysql_connect($host,$user,$pass);
// Select the database
$database = mysql_select_db($database,$connection);
//open text file
$file = file('names.txt');
//doing loop in the text file lines
foreach ($file as $line_num => $line)
{
$who = $line;
//DB SQL statment
$query = "SELECT * FROM list WHERE name='$who'";
// Make the query
$result = mysql_query($query);
// Count the rows (total result)
$row = mysql_num_rows($result);
// For each result
while($row = mysql_fetch_array($result))
{
$name = $row['name'];
$sex = $row['sex'];
$filedata = $name. " is ". $sex;
echo $filedata;
}//while
$fp = fopen( "sex.txt" , "a" );
fwrite( $fp, $filedata);
echo $who. " is Unkown";
echo "<br />\n";
}//foreachCode: Select all
john
kate
mary
edward
shanter
tony
meg
kindey
george
sarah
mike
fedCode: Select all
john is male
kate is female
mary is female
edward is male
shanter is male
tony is male
meg is female
kindey is Unkown
george is Unkown
sarah is female
mike is male
fed is UnkownCode: Select all
john is male
kate is femaleis Unkown
mary is femaleis Unkown
edward is maleis Unkown
shanter is maleis Unkown
tony is maleis Unkown
meg is femaleis Unkown
kindey is Unkown
george is Unkown
sarah is femaleis Unkown
mike is maleis Unkown
fed is Unkown