0

I'm trying to read temperature from sensor DHT22 connected to raspberry pi and write it to mysql DB via PHP script.

I'm getting the temperature like this:

$temp = shell_exec('./temp.py');

Script returns number like 45 or 45.7 etc... The same as humidity.. When I echo it in the same script, it works. And the thing, which I cannot pass through is here:

$sql='INSERT INTO values (id, date, time, timestamp, temp, hum) VALUES ("", NOW(), NOW(), NOW(), "$temp", "$hum")';
$result = MySQL_Query($sql);

But in DB is always written $temp and $hum, not the actual values. I think that I've already tried all possible combinations of quotation marks, etc... What can be possibly wrong? Thanks in advance for the answers.

Hixo
  • 3
  • 2

1 Answers1

0
$sql="INSERT INTO values (id, date, time, timestamp, temp, hum) VALUES (\"\", NOW(), NOW(), NOW(), \"$temp\", \"$hum\")";
$result = MySQL_Query($sql);

Notice the double quotes.

There is a difference between double and single quotes:

  • Double quotes are parsed by the php engine. You can put variables inside them, and can be used to display escaped special characters.
  • Single quotes, on the other hands, are displayed as they are. They are not parsed by PHP and do not require escaping, except for the ' to tell the interpreter whether the quote is the end of the string or part of it.

You were using single quotes, that's why your string isn't parsed into variables.

Further information could be found in the PHP Manual

Wahib Mkadmi
  • 627
  • 2
  • 9
  • 26