Mysql query, successful?

I’m playing around with the database mysql part of Kirby.

I’m running a raw query because I want to use DELETE with IN.

$sql = "DELETE from products WHERE id IN ('" . $array_with_ids . "');";
print_r( db::query($sql) );

The result I get is not true or false, which is expected because the query expects a database result.

Collection Object ( [pagination:protected] => [data] => Array ( ) )

The above looks ok, because I did not use a SELECT. But how can I know if it failed? Will it return it differently?

If the query fails, I want to do something, like mail myself or store it in a page.

You are looking for db::execute. However, your raw query has a huge SQL injection issue. Never add strings to a SQL query without escaping.

Kirby’s API also supports the IN operator, which is much safer as it escapes everything for you:

$result = db::table('products')->where('id', 'in', $array_with_ids)->delete();
3 Likes

Thank you so much! That looks really awesome! I did not find it in the docs. RTFM? If I could find it. :stuck_out_tongue:

The advanced behavior isn’t all documented on the site, but you can take a look at the source code. :slight_smile: