Jump to content

PHP Script not working properly


phpstarter

Recommended Posts

Hi All,
I'm a new starter with PHP. Need urgent help with the following script as it does not make sense to me. Your help will be much appreciated in advance.

<?

$connection = mysql_connect("host", "username", "pwd")
or die("Couldn't connect.");

$db = mysql_select_db("database", $connection)
or die("Couldn't select database.");

$filename = "info.txt";
$fd = fopen ($filename, "r");
$contents = fread ($fd,filesize ($filename));

fclose ($fd);
$delimiter = "\n";
$splitcontents = explode($delimiter, $contents);

$counter = "0";
?>

<br><br>

<?
foreach ( $splitcontents as $key => $value )
{

$sql = "SELECT * FROM table where field = \"$value\"";

$sql_result = mysql_query($sql,$connection) or die(mysql_error());

$row = mysql_fetch_array($sql_result);
$sku = $row["sku"];
$mpn = $row["mpn"];

echo "
<table border=1>
<tr>
<td>$counter</td>
<td>$sku</td>
<td>$mpn</td>
</tr>
<tr><td><b>Split $counter: </b> $value\n<br></td></tr>
</table> ";
$counter = $counter+1;
mysql_free_result($sql_result);

}

mysql_close($connection);

?>

The Info.txt file has following values:
n03-00032
n04-00033
n05-00034
.
.
.
.
n70-00069

The output comes as follows. I can not see any error in the script. Just don't know why it is not working. It is supposed to get values from Info.txt and with the help of mysql query display all values one by one but it only displays last value.


0
Split 0: n03-00032

1
Split 1: n04-00033

2
Split 2: n05-00034
.
.
.
.
70 n70-00069 A134ABU
Split 70: n70-00069

If anyone see any error or has any idea of solving this problem then pls let me know.

Link to comment
Share on other sites

Try making this ammendment:
[code]while($row = mysql_fetch_array($sql_result)) {
    $sku = $row["sku"];
    $mpn = $row["mpn"];

    echo "
    <table border=1>
    <tr>
    <td>$counter</td>
    <td>$sku</td>
    <td>$mpn</td>
    </tr>
    <tr><td><b>Split $counter: </b> $value\n<br></td></tr>
    </table> ";
    $counter = $counter+1;
}[/code]
Link to comment
Share on other sites

[!--quoteo(post=381883:date=Jun 9 2006, 04:20 PM:name=Fyorl)--][div class=\'quotetop\']QUOTE(Fyorl @ Jun 9 2006, 04:20 PM) [snapback]381883[/snapback][/div][div class=\'quotemain\'][!--quotec--]
To separate a file into an array by lines you don't need to use fread and explode, just use $array = file('info.txt'); and each array value will be a new line. You can then work with that.
[/quote]

Thanks for quick response but your given style comes up as blank page in browser. There are no values displayed at all. Any suggestions.......
Link to comment
Share on other sites

That suggests there's something wrong with the file. You might not have read permissions.[code]$array = file('info.txt');
echo "<pre>";
var_dump($array); // see if it's worked
echo "</pre>";
foreach($array as $line)
{
// Do whatever you need to do to each line here using $line
}[/code]
Link to comment
Share on other sites

[!--quoteo(post=381901:date=Jun 9 2006, 10:43 AM:name=joquius)--][div class=\'quotetop\']QUOTE(joquius @ Jun 9 2006, 10:43 AM) [snapback]381901[/snapback][/div][div class=\'quotemain\'][!--quotec--]
^ sorry this post wasn't here when i was typing

remember you need to split the value of array[linenum]

foreach ($array as $key => $value)
{
function my_BossSaysI_CanGoHome_now () // or whatever you need to do with the line
}
[/quote]
foreach($array as $key => $value) is not needed in this instance as the $key is useless for this loop, all that's needed is the value which can be obtained by foreach($array as $value)
Also, if you're calling a function my_BossSaysI_CanGoHome_now(), it won't be able to do anything to the line because it doesn't have anything passed to it. If it's going to affect the line you need to pass it to the function like my_BossSaysI_CanGoHome_now($line);
Link to comment
Share on other sites

[!--quoteo(post=381907:date=Jun 9 2006, 04:51 PM:name=Fyorl)--][div class=\'quotetop\']QUOTE(Fyorl @ Jun 9 2006, 04:51 PM) [snapback]381907[/snapback][/div][div class=\'quotemain\'][!--quotec--]
foreach($array as $key => $value) is not needed in this instance as the $key is useless for this loop, all that's needed is the value which can be obtained by foreach($array as $value)
Also, if you're calling a function my_BossSaysI_CanGoHome_now(), it won't be able to do anything to the line because it doesn't have anything passed to it. If it's going to affect the line you need to pass it to the function like my_BossSaysI_CanGoHome_now($line);
[/quote]


Dear Fyorl,
I have tried exactly the way you have mentioned in your post as below.

$array = file('info.txt');
echo "<pre>";
var_dump($array); // see if it's worked
echo "</pre>";
foreach($array as $line)
{
My code to check database for items through text file.
}

I have checked the info.txt file also and it is on file is ready for archiving which means it has full read and write permission.

This code bring out the same result i.e., it reads text file, displays each product part number from text file but only displays the values from database for last text file entry. I really do not understand what is the problem. Any more suggestions please.
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.