1

I have a database named teste with a table called exemplo with 2 columns (id, nome): Want a php script to select all the information in the nome column, and echo that information and place them in a txt file. What's wrong in the code? I can only see the first value and not the second.. And with an infinite loop!

$link = mysql_connect('localhost', 'root', '123');
if (!$link) {
  die('Could not connect: ' . mysql_error());
}

mysql_select_db('teste', $link);

/* Desired */
$file = fopen("myfile.txt","w");

$result = mysql_query("SELECT * FROM exemplo");
$array = mysql_fetch_array($result);
while ($array) {
  echo $array['nome'];

  fputs($file ,$array['nome']); 
}

mysql_close($link);
0

5 Answers 5

3
    $array = mysql_fetch_array($result);

This only fetches one row of results. It doesn't stuff the entire result set into your array. You need to do:

while($row = mysql_fetch_array($result)) {
   echo $row['nome'];
   fputs($file, $row['nome']);
}
Sign up to request clarification or add additional context in comments.

Comments

0

That's because mysql_fetch_array only returns one result at a time, so $array is always true. You want this:

while ($array = mysql_fetch_array($result)) {
  echo $array['nome'];
  fputs($file ,$array['nome']); 
}

1 Comment

and to add a new line and not see line1line2 in a row?
0

You assign $array once and than loop over it in while($array) so this keeps running and running as $array is unchanged! Try changing your code like that:

<?php

        $link = mysql_connect('localhost', 'root', '123');
        if (!$link) {
                die('Could not connect: ' . mysql_error());
        }

        mysql_select_db('teste', $link);

        /* Desired */

        $file = fopen("myfile.txt","w");

        $result = mysql_query("SELECT * FROM exemplo");
        while ($array = mysql_fetch_array($result)) {
        echo $array['nome'];

        fputs($file ,$array['nome']); }
        mysql_close($link);
?>

1 Comment

Remove the ; from the while loop condition. while ($array = mysql_fetch_array($result);)
0

This should work. you were leaving fputs out of the data loop.

<?php

$link = mysql_connect('localhost', 'root', '123');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('teste', $link);

/* Desired */

$file = fopen("myfile.txt","w");

$result = mysql_query("SELECT * FROM exemplo");
while($array = mysql_fetch_array($result))
{
    echo $array['nome'];
    fputs($file, $array['nome'] ."\n");
}

mysql_close($link);

Comments

0

mysql_fetch_array returns a single row. You need to call it in a loop:

while ($array = mysql_fetch_array($result)) {
    echo $array['nome'], "\n";
    // do more stuff
}

2 Comments

and to add a new line and not see line1line2 in a row?
for that you need to add a line separator yourself

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.