Based on source you posted on github > , in this section:
$pdo = new PDO("mysql:host=localhost;dbname=angularDBB"; "root", "root", $opcoes);
$opcoes = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
1st Step
First, you should change the order:
$opcoes = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
$pdo = new PDO("mysql:host=localhost;dbname=angularDBB"; "root", "root", $opcoes);
Because when you create a PDO instance in the $pdo
variable, it tries to know if the $opcoes
variable is set, and what its values are.
Second Step 2
Still in this same part of the code, we have an error writing the parameters. Where should it be:
$pdo = new PDO ('mysql: host= host_name; dbname= db_name;', 'db_user', 'db_pwd', [...]);
You're like this:
$pdo = new PDO ('mysql: host= host_name; dbname= db_name'; 'db_user', 'db_pwd', [...]);
^
There is a ;
instead of a comma. Removing ;
would be enough to fix this line.
So far we have practically solved the problem, but now let's analyze.
With the modifications made, if we do:
var_dump(conectar());
// Retorna: object(PDO)#1 (0) { }
This means that you did not have any object return, this is because we are trying to find out if it has returned something other than boolean , you can see this by the braces.
Now, if we try to reprint the value returned by the function, but this time using echo , and using the cast type method to set the expected return type:
echo((bool)conectar());
// Retorna: 1, equivalente a true
Another note is in this part of the code:
print_r($pdo->query('select database()')->fetch());
It is very unlikely that we will get any correct result with this, in an attempt to return all existing results, because by writing the whole expression in line, we get an infinite return , see in this example where I used the jogos
table as a base:
while($linha = $pdo->query("SELECT * FROM jogos")->fetch(PDO::FETCH_OBJ)){
echo $linha->nome . "<br/>";
}
In addition to not moving the pointer once, while selecting results in the database, it prints looping infinity of the first result.
The correct one would be to first query, and then return the results using fetch
, if the goal is to return all the results in the table in use.
$query = $pdo->query("SELECT * FROM jogos");
while($linha = $query->fetch(PDO::FETCH_OBJ)){
echo $linha->nome . "<br/>";
}
For the case of a single result, as it is in your original example we would do the following:
$pdo->query("SELECT * FROM jogos")->fetch(PDO::FETCH_OBJ)->nome;
// fetch(tipo de retorno)
// nome é o indice que queremos retornar
The full function would look like this:
function conectar(){
$host = "localhost";
$user = "root";
$pass = "root";
$dbname = "example";
$erro = "";
try {
$opcoes = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
$pdo = new PDO("mysql:host=localhost;dbname=example", "root", "root", $opcoes);
} catch (Exception $e) {
$erro = $e->getMessage();
}
if($pdo){
return $pdo->query("SELECT * FROM jogos")->fetch(PDO::FETCH_OBJ)->nome;
// Ou usando o looping while, para retornar todos os resultados
/*
$query = $pdo->query("SELECT * FROM jogos");
while($linha = $query->fetch(PDO::FETCH_OBJ)){
echo $linha->nome . "<br/>";
}
*/
} else {
return $erro;
}
}
// Saída dos resultados
echo conectar();
Still, this function is by far the best practice for instantiating PDO connections, I recommend you look for a more appropriate way to do this.
Some references:
How to properly set up a PDO Connection - SOen
PDO PHP Class - Culttt
PDO - PHP.net