I have an ongoing side project to write some of the Gamium Recommendation Engine tooling in Peachpie, a .NET compiler for PHP.
This is hindered by the fact that I know .NET very little, so I’m constantly learning new things… which frankly is part of the motivation for the project.
The first bit of Peachpie I needed to get to work is connection to the database. The core of the Gamium Recommendation Engine depends on a handful of tables in a MariaDB database. So I need to be able to talk to MySQL.
Peachpie supports MySQL and PDO. PHP Data Objects is a library that provides a nice middle ground between low-level database operations and a full-fledged ORM or DataMapper. Because the tables are small in scope it’s frankly easier to handle them manually than using a complex database abstraction library.
However, try as I might I couldn’t get Peachpie to recognize the PDO functionality the website assured me exists. Come to find out there’s a couple of steps involved… and I was doing it all wrong.
Let me save you the headache. It’s really, really easy to make it work.
In your .msbuildproj file, in the <ItemGroup> block, you need to add a reference to the PDO package, like so:
<Project Sdk="Peachpie.NET.Sdk/0.9.970">
...
<ItemGroup>
<Compile Include="**/*.php" />
<PackageReference Include="Peachpie.Library.PDO.MySQL" Version="0.9.970" />
</ItemGroup>
...
</Project>
Now here’s where I made my blunder. There’s examples on the web showing you need to add this reference… but they didn’t explain what it meant. So like any newbie, I copied the code block in and it didn’t work!
Here’s the trick, and it’s so obvious I’m embarrassed to have to admit to having missed it, and it’s this:
The version number matters.
You need to make sure the “Version” in the PackageReference matches the version of the Project SDK. In my case, the copy-pasted PackageReference was wrong, and this caused Peachpie to silently not load the PDO MySQL driver.
There was no error message, it just didn’t work.
Now you know! Go forth and connect to your database in Peachpie. And laugh a little at my expense whilst doing so.