<p>You can't put PHP code into Twig, and there's no straightforward way to write custom DB queries in Twig. That's by design – Twig is a templating language, not meant for custom queries or processing.</p>
<p>Instead, you should provide a <a href="https://craftcms.com/docs/3.x/extend/services.html" rel="nofollow noreferrer">service object</a> with utitily methods that you can call from Twig. Depending on your use-case, you can accept parameters (for example, arguments to a query) to get the flexibility you need.</p>
<p>First, look at the link above to learn more about services. If you're building a <a href="https://craftcms.com/docs/3.x/extend/plugin-guide.html" rel="nofollow noreferrer">plugin</a>, your service class goes in there. If you're working on some site-specific functionality, you can put it in a <a href="https://craftcms.com/docs/3.x/extend/module-guide.html" rel="nofollow noreferrer">module</a> instead. Once you have defined your service class and added it as a component to your plugin or module, you need to make it available to Twig – see the documentation on <a href="https://craftcms.com/docs/3.x/extend/extending-twig.html" rel="nofollow noreferrer">Extending Twig</a>. For a plugin, extending the <code>CraftVariable</code> would be the most common way to provide a custom service object to Twig. In a module, I'd use a Twig extension instead, though that's mostly preference.</p>

CC BY-SA 4.0
2021-11-02T09:50:09.717Z
Thank you @MoritzLost
it's all clear now, thanks to you.
Ra3d K
CC BY-SA 4.0
2021-11-02T08:18:15.410Z
2021-11-02T08:18:15.410Z
MoritzLost
<p>if i have a custom table (ct_table) and i have to use MySQLi commands on it using the PHP syntax,
it won't work if i put the PHP snippets inside a twig file.</p>
<p>So are there any tools or official methods to convert PHP syntax DB-commands to TWIG syntax?
or even let the Twig engine understand the PHP code?</p>

<craft3><templating><database><php>
how to write db-commands on custom table (PHP syntax) inside twig file?
# how to write db-commands on custom table (PHP syntax) inside twig file?

(Asked by: username_0 on 2021-11-02)

if i have a custom table (ct\_table) and i have to use MySQLi commands on it using the PHP syntax,
it won't work if i put the PHP snippets inside a twig file.

So are there any tools or official methods to convert PHP syntax DB-commands to TWIG syntax?
or even let the Twig engine understand the PHP code?

**Tags:** craft3, templating, database, php

---

## 1 answers

### Answer by username_1 on 2021-11-02. Score: 2 | Accepted answer

You can't put PHP code into Twig, and there's no straightforward way to write custom DB queries in Twig. That's by design – Twig is a templating language, not meant for custom queries or processing.

Instead, you should provide a [service object](https://craftcms.com/docs/3.x/extend/services.html) with utitily methods that you can call from Twig. Depending on your use-case, you can accept parameters (for example, arguments to a query) to get the flexibility you need.

First, look at the link above to learn more about services. If you're building a [plugin](https://craftcms.com/docs/3.x/extend/plugin-guide.html), your service class goes in there. If you're working on some site-specific functionality, you can put it in a [module](https://craftcms.com/docs/3.x/extend/module-guide.html) instead. Once you have defined your service class and added it as a component to your plugin or module, you need to make it available to Twig – see the documentation on [Extending Twig](https://craftcms.com/docs/3.x/extend/extending-twig.html). For a plugin, extending the `CraftVariable` would be the most common way to provide a custom service object to Twig. In a module, I'd use a Twig extension instead, though that's mostly preference.

> **Comments:**
> **username_0**: Thank you @MoritzLost it's all clear now, thanks to you.  

---

