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.