0

I need to output a name from the last added row of my table, and it works sort of, it outputs the correct data twice. So instead of name1 it gives me name1name1. I'm still a novice with PHP but i think there is a double loop somewhere in this piece of code which is giving me a hard time, but i'm still unable to locate where that is..

If someone can give me a pointer that would be greatly appreciated.

    <?php $query="SELECT question FROM poll ORDER BY id DESC LIMIT 1";
    $results = mysql_query($query);

    while ($row = mysql_fetch_array($results)) {
       echo '<tr>';
          foreach($row as $field) {
       echo '<td>' . htmlspecialchars($field) . '</td>';
    }
       echo '</tr>';
    } ?>
user2099810
  • 361
  • 5
  • 15
  • Can you give us the query output? – EnriMR Jun 12 '15 at 09:42
  • Have a look at [mysql_fetch_array() doc](http://php.net/manual/en/function.mysql-fetch-array.php) and change the `$result_type` paramater to either `MYSQL_ASSOC` or `MYSQL_NUM`. –  Jun 12 '15 at 09:44
  • 3
    And though it's not directly related to your question, you might want to read this post: http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php –  Jun 12 '15 at 09:45
  • Note that the mysql extension has been removed from PHP. – hek2mgl Jun 12 '15 at 12:15

3 Answers3

3

By default, mysql_fetch_array() returns values both as associative and as enumerated (MYSQL_BOTH); specify either one or the other

while ($row = mysql_fetch_array($results, MYSQL_ASSOC)) {

EDIT

But the mysql extension is old and deprecated, and you really should be switching to mysqli or pdo with prepared statements and bind variables. If you're just learning, then it's best to learn the right methods with the right extensions from the start

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • Thanks Mark i changed my code to: '; echo '' . htmlspecialchars($row['question']) . ''; echo ''; } ?> And now it works :D – user2099810 Jun 12 '15 at 09:46
  • You can use [mysql_fetch_assoc()](http://php.net/manual/en/function.mysql-fetch-assoc.php) if you don't want to copy the `MYSQL_ASSOC` parameter everywhere. –  Jun 12 '15 at 09:48
  • @MarkBaker You should know that `mysql_fetch_array()` was a long time deprecated and has been remove from PHP since a time. Why do you post answers like this? Don't you care about quality at all? – hek2mgl Jun 12 '15 at 12:14
  • @hek2mgl - So the part of my answer about old/deprecated etc, and switch to mysqli or pdo, counts for nothing? – Mark Baker Jun 12 '15 at 12:19
  • It counts but the question is definitely a close candidate. When answering such question you encourage people to post such things again and again. Especially the PHP tag has serious quality problems! But why? PHP is such a cool language! Probably we need to teach people to ask good questions? You, having such a high reputation in this tag and hanging around frequently, are a person who could take more responsibility for that. – hek2mgl Jun 12 '15 at 12:24
2

No need of the while. You are fetching a single row. And use mysql_fetch_assoc for associative array

$row = mysql_fetch_assoc($results);

echo '<tr>';
foreach($row as $field) {
   echo '<td>' . htmlspecialchars($field) . '</td>';
}
echo '</tr>';
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
1

I would use mysql_fetch_assoc. Because this will only fetch the results as an associative array.

$query="SELECT question FROM poll ORDER BY id DESC LIMIT 1";
$results = mysql_query($query);

while ($row = mysql_fetch_assoc($results)) {
   echo '<tr>';
   foreach($row as $field) {
       echo '<td>' . htmlspecialchars($field) . '</td>';
   }
   echo '</tr>';
}
Andizer
  • 344
  • 1
  • 7