Based on the source that you posted on github, in this passage :
$pdo = new PDO("mysql:host=localhost;dbname=angularDBB"; "root", "root", $opcoes);
$opcoes = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
First Step
First, one must 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 an instance of PDO in the variable $pdo
she tries to know if the variable $opcoes
is defined, and what are its values.
2nd Step
Still in this same part of the code, we have an error in the writing of the parameters. Where it should be:
$pdo = new PDO ('mysql: host= host_name; dbname= db_name;', 'db_user', 'db_pwd', [...]);
It’s like this :
$pdo = new PDO ('mysql: host= host_name; dbname= db_name'; 'db_user', 'db_pwd', [...]);
^
There is a ;
instead of a comma. Remove the ;
would be enough to correct this line.
So far we have practically solved the problem, but now let’s review.
With the modifications made, if we do:
var_dump(conectar());
// Retorna: object(PDO)#1 (0) { }
Which means there was no object return, which is why we’re trying to figure out if any that aren’t returned Boolean, You can see that by the braces.
Now, if we try to print again the value returned by the function, but this time using the echo, and using the method cast type to define the expected return type:
echo((bool)conectar());
// Retorna: 1, equivalente a true
Another observation 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 from this, in an attempt to try to return all the existing results, because by writing the entire expression in line, we get a infinite return, see in this example, where I used the table jogos
as a basis:
while($linha = $pdo->query("SELECT * FROM jogos")->fetch(PDO::FETCH_OBJ)){
echo $linha->nome . "<br/>";
}
In addition to not moving the pointer once, during the selection of results in the database, prints looping infinite of the first result.
The correct would be to first perform the query, and only then return the results using the fetch
, if the goal was to return all 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 connections PDO, I recommend you look for a more appropriate way to do so.
Some references:
How to properly set up a PDO Connection - Soen
PDO PHP Class - Culttt
PDO - PHP.net
Glad you started with PDO. Remove those lines with
mysql_*
, add this in the builder.$opcoes = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION); new PDO(...., "root", $opcoes);
– rray
dbname:angularD
has that:
really? should be a=
,dbname=angularD
– rray
Telvez (and I said maybe...) the problem is in the syntax try this way $Pdo = new PDO( 'mysql:host=localhost;dbname=dbname:angularDB', 'root', 'root' );
– Paulo Roberto
@rray, look at the changes I made to the code in the post... I made the changes, I just invented a name for the bank and even then, no error is displayed on the screen.
– GustavoSevero
Have you tried doing
debug
?var_dump($pdo)
, and see what comes back. And what you’re using is a function ?– Edilson
Even with var_dump($Pdo) nothing appears.
– GustavoSevero
Pass the full script, so we can understand what the problem is.
– Edilson
Gustavo, just one exercise: read only the title of this question, can you see any meaning? Neither do I... Please, gives a fancy on this, it costs very little to write a descriptive title of the problem, being that "problem with X" is a bad title :P
– brasofilo
@Brasofilo, only you and Edilson did not understand my problem. And I describe my problem in the post itself! Your comment was totally unnecessary.
– GustavoSevero
I didn’t even look at your problem, I just looked at the title. It’s the title of your question that appears on the first page. If you want to attract the attention of people with know-how, Priche in the title, if not, all right, no problem. . . . Ah, yeah, a descriptive title helps other people who are having the same problem find the solution.
– brasofilo
title suggestion:
Conexão com PDO não dá erro, nem com banco ficticio
– brasofilo
Pronto @brasofilo, I changed the title
– GustavoSevero
@Edilson, I put my code in git https://github.com/GugaSevero/PDO
– GustavoSevero
There’s the answer to your problem.
– Edilson
Just to clarify, I try to attack content problems, and the more professional I think the person is more direct I will be; attacking people is trolling and is not my thing :)
– brasofilo