0

I have database with five projects added by username. When I want to get project by user returns me only one project.

      <?php
      require_once("db.php");
      $db = DB();
      $query = $db->prepare("SELECT * FROM projects WHERE username='$username'");
      $query->execute();
      $row = $query->fetch();
      $name = $row['name'];
      $project = $row['project'];
    echo "<p>Project name: $name</p>
          <p>Project: $project</p>
        ?>
Keepd
  • 21
  • 1
  • 1
  • 5
  • Fetch using `while` – u_mulder Mar 25 '17 at 19:57
  • change this ` $row = $query->fetch();` to ` $row = $query->fetchAll();` then loop with `while` or `foreach` loop. Fetch is returns you only 1 result – Mario Mar 25 '17 at 19:58
  • btw; you don't need the `prepare/execute`, you can just do `query()`. However, you're not "preparing" anything *per se* yet you should and use a prepared statement. What you have now doesn't qualify as a prepared statement to which you're open to an sql injection. – Funk Forty Niner Mar 25 '17 at 20:02
  • RTM on PDO's `fetch()` http://php.net/manual/en/pdostatement.fetch.php – Funk Forty Niner Mar 25 '17 at 20:03
  • Damn. So easy... Thank you! – Keepd Mar 25 '17 at 20:07
  • Plus, if that's your real code `echo "

    Project name: $name

    Project: $project

    ` is missing a quote and a semi-colon and would throw a parse error.
    – Funk Forty Niner Mar 25 '17 at 20:08
  • I forgot to add them. Now my code is `$sql = "SELECT * FROM projects WHERE username = '$username'"; foreach($db->query($sql) as $row)` Should I be worried about sql injection? – Keepd Mar 25 '17 at 20:20

0 Answers0