Error with query ();

1

Good morning, I've been with this error for a while and I'm unable to solve it to see if anyone can help me out, I'd really appreciate it!

$base=new PDO('mysql:host=localhost; dbname=pruebas', 'root', '');
$base->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$base=exec("SET CHARACTER SET UTF8");

The problematic line is this (18):

$registros=$base->query('SELECT * FROM datos_usuarios') >fetchAll(PDO::FETCH_OBJ);

and this is the error that gives me:

  

Fatal error: Call to a member function query () on string in   C: \ wamp64 \ www \ php \ CRUD \ index.php on line 18

Thank you very much!

    
asked by Xavi 06.07.2017 в 10:54
source

1 answer

2

You are assigning the result of the exec to $ base ("SET CHARACTER SET UTF8") and this returns a string not the PDO object.

The solution is to change the = by the method access date - >

$base->exec("SET CHARACTER SET UTF8");

With this your code would be like this:

$base=new PDO('mysql:host=localhost; dbname=pruebas', 'root', '');
$base->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$base->exec("SET CHARACTER SET UTF8");
$registros=$base->query('SELECT * FROM datos_usuarios') >fetchAll(PDO::FETCH_OBJ);
    
answered by 06.07.2017 / 16:42
source