-1

I need some help here. I have some code below, and my question is, I'm not getting where the error is, which is showing by the error message.

try
{
    $db = new PDO('mysql:host=localhost; db_name=softdemo','root', '');
}   
catch(PDOException $e)
{
    echo $e->getMessage();
}

try{
    foreach ( $db->query("SELECT * FROM users") as row )
    {
        echo $row['username'].$row['password']."<br/>";
    }
    $db = null;
}
catch(Exception $e)
{
    echo $e->getMessage();      
}
sharif
  • 1
  • 1
  • 2
  • 3
    `row` would be `$row`!! – Saty Apr 07 '16 at 06:31
  • 1
    These kinds of errors should be solved with speicalized IDEs like PHPStorm, Eclipse or NetBeans. These are very common and should not be asked here. Solving these errors on the own improves debugging capability. – Pupil Apr 07 '16 at 06:35
  • thnx for ur argument... :) – sharif Apr 07 '16 at 07:00

3 Answers3

1

Error comes from this line you missed $ before $row variable

  foreach ( $db->query("SELECT * FROM users") as row )

Change this to

$res = $db->query("SELECT * FROM users");
if(!empty($res))
{
  foreach( $res as $row )
  { //code here..
  }
}
Vishnu Sharma
  • 612
  • 4
  • 17
Maninderpreet Singh
  • 2,450
  • 2
  • 15
  • 29
1

You've missed $ sign, this is required as $row is a variable.

So, Change

foreach ( $db->query("SELECT * FROM users") as row )

To

$res = $db->query("SELECT * FROM users");
if(!empty($res))
{
  foreach( $res as $row )
  { //code here..
  }
}
Vishnu Sharma
  • 612
  • 4
  • 17
1

You've missed out a $, this is required as $row is a variable.

foreach ( $db->query("SELECT * FROM users") as $row )
Panda
  • 6,824
  • 6
  • 34
  • 49