M
M
Mesuti2020-04-20 17:26:05
PHP
Mesuti, 2020-04-20 17:26:05

How to pass a variable to SQL query?

The code works fine

echo  json_encode(R::getAll('SELECT * FROM `category` WHERE `id_user` = 100'));


How to replace 100 with a variable?

This returns an empty array
$id_user = 100;
echo  json_encode(R::getAll('SELECT * FROM `category` WHERE `id_user` = $id_user'));

Answer the question

In order to leave comments, you need to log in

3 answer(s)
F
FanatPHP, 2020-04-20
@Mesuti

Who teaches you redbin, without even giving the basic syntax?
A variable is never passed directly to a request. It must be submitted separately.

echo  json_encode(R::getAll('SELECT * FROM `category` WHERE `id_user` = ?', [$id_user]));

R
Rsa97, 2020-04-20
@Rsa97

A single-quoted string does not expand the variable.

H
hesy, 2020-04-20
@hesy

Try SQL in double quotes " .

"SELECT * FROM `category` WHERE `id_user` = $id_user"

or
'SELECT * FROM `category` WHERE `id_user` = ' . $id_user

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question