-4

I give me this error that i dont have seen: 'Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\sito\personalImage.php on line 17' this is my code:

try{
    $conn= new PDO("mysql:host=localhost;dbname=first","root","..");
    echo "connection good";
    $user=$_COOKIE['log'];
    $conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    foreach($conn->exec('SELECT cognome FROM utenti') as $row){

    }

    $conn=null;
}
catch(PDOException $e){
    echo $e->getMessage();
}

connection to the database is good. Someone can give me an explanation

thorny84
  • 23
  • 6
  • Are you sure you have results? Check `mysql_num_rows()` before trying to access the result set. – John Conde May 19 '15 at 12:59
  • 1
    There are soooo many wrong things here... – Masiorama May 19 '15 at 12:59
  • Add `or die(mysql_error())` to `mysql_query()`. – Funk Forty Niner May 19 '15 at 13:00
  • you should refer how to execute the query and how to get the results @thorny84 – Pathik Vejani May 19 '15 at 13:02
  • [Your script is at risk for SQL Injection.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and consider using PDO, [it's not as hard as you think](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 19 '15 at 13:02
  • nothing error in mysql_error() – thorny84 May 19 '15 at 13:31

1 Answers1

0

test the number of result with mysql_num_rows and then get your result.

Also 2 things very important:

  1. replace your ' by " and your " by ', because a variable can only be interpreted inside a string if this string is delimited by " (french speaker,dont know the name, please corrector replace all this blabla by the real name, thanks :) )
  2. mysql_ functions are deprecated you should no more use it, use mysqli or pdo instead (i recommend pdo).

there are both very important!

buona fortuna amico mio

Fabrice Kabongo
  • 672
  • 10
  • 22