Score:0

PHP connection to Maria DB

lk flag

Hopefully someone can explain an issue I found doing a simple connection between PHP 7 and MariaDB Server 10.3.32 on Ubuntu 20.04.

Here is the connection data:

$dsn = "mysql:localhost; dbname=databasename; charset=utf8mb4";  
$options = [ PDO::ATTR_EMULATE_PREPARES => false,  
             PDO::ATTR_ERRMODE => PDO::ERRORMODE_EXCEPTION,   
             PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
            ];
$pdo = new PDO($dsn, "username", "password", $options);

The $options needed to be removed before it would output string data to the program. Without doing this, I would just get black boxes which I assume was some type of data, but not the strings I was looking for.

I tried commenting out PDO::ATTR_DEFAULT_FETCH_MODE=>PDO::FETCH_ASSOC, but I still get the problem. I found that if I comment out PDO::ATTR_DEFAULT_FETCH_MODE=> PDO::FETCH_ASSOC I can use a for loop to get the information with number index $dvds[0][0]; but I cannot use the name of the database column ie. $dvds['title']; if I leave it uncommented I can get it with print_r but I don't get anything in the for loop using number or associative index. Weird! I found out that I was not accessing the data being returned properly. I needed to use a foreach loop then access each row by the associative title of each column in the database. Thanks.

Mike G avatar
lk flag
it should read $dsn = "mysql:host=localhost; dbname=databasename;charset=utf8mb4";
Mike G avatar
lk flag
I found that if I comment out PDO::ATTR_DEFAULT_FETCH_MODE=> PDO::FETCH_ASSOC I can use a for loop to get the information with number index $dvds[0][0]; but I cannot use the name of the database column ie. $dvds['title']; if I leave it uncommented I can get it with print_r but I don't get anything in the for loop using number or associative index. Weird!
Score:0
lk flag

After a week of trying things I finally found that the issue was the CSS code had all tables background in black and the text was black so nothing was visible. I can't believe it but this was the issue from the beginning. I feel silly posting this but maybe it will help someone else. Thanks.

Score:-1
ec flag

is it possible you are missing host= on your $dsn assignment?

like:

$dsn = "mysql:host=localhost; dbname=databasename; charset=utf8mb4";
Mike G avatar
lk flag
That does not seem to be the issue because when I comment out the options the connection works and provides the expected data. Thanks for your suggestion.
Mike G avatar
lk flag
When I typed the question I forgot to enter host but it is in the string in the program. Thanks for pointing it out. I still don't know why the options are not right though. Thanks
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.