PHP connection to Maria DB

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.

2

2 Answers

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.

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

like:

$dsn = "mysql:host=localhost; dbname=databasename; charset=utf8mb4";
2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like