I recently started using Medoo it’s a lightweight PHP database framework/wrapper, so far it has helped me on my side projects and it has certainly accelerated parts of the development.
Although the documentation is good, there are a few things you have to look very closely to find. Since I have found them I will share it here so that it can help you.
Get a single row without looping using Medoo
In the Select example it shows you how to print the rows using a for-loop. However if you are only retrieving one row there isn’t a need for a loop and it can be accessed by the array index like:
$data = $database->select("account", [
"user_name",
"email"
], [
"user_id" => 100
]);
Instead of looping it you can get access to the value like this and print it:
$data[0]['user_name']
The recommended way to get one row is by using the GET object:
$data = $database->get("account", ["name","email"], [
"user_id" => 1234
]);
Then access it like:
$data['name'];
$data['email'];
Simple way to use PDO prepared statements with Medoo
You can use the default prepared statements in Medoo or if you have already sanitized your input then you can also use the query object:
$datas = $database->query(
"SELECT models FROM cars WHERE <make> = :make ORDER BY models ASC", [
":make" => "Ford"
]
)->fetchAll();
Then loop through it and print out the values. I find this easier because I can just insert my raw SQL query with the placeholders in between <>
e.g. <make> = :make
This saves me writing the query in the Medoo way. If you needed more complex things like tables joins or full text searching in BOOLEAN MODE then I find this to be the easier way.
Full Text Searching with Prepared Statement using Medoo
Although medoo supports many complex WHERE conditions including Full text searching, I couldn’t find any examples of it using prepared statements, however it is possible to use the query object as above.
For example if I wanted to use match against using boolean mode I can easily do this using the query object.
$data = $database->query(
"SELECT COUNT(models) AS count FROM makes WHERE MATCH(<models>) AGAINST (:q IN BOOLEAN MODE)", [
":q" => $search
]
)->fetchAll();
I am still using prepared statements but using the raw query which I am comfortable with otherwise I would have to do it the Medoo way which can be hard to read.
I hope this helps, if you have tricks using Medoo then leave a comment. If you prefer using an interface to interact with your database then have a look at this handy tip about find and replace using phpMyAdmin only.
Been looking around the web for medoo and full-text search. Thank you for this and I hope you will share more on medoo. It’s a great tool but not enough documentation yet.