Grynets Grynets - 7 months ago 190
SQL Question

Telegram bot can't receive data from table with mysqli

I'm creating bot for Telegram.


And everything was OK, until I started to connect my bot with my database.
And I want to make my bot get schedule by data from table.


But it can't. And I don't know why.


Here is a part of my code which belongs to DB connection.


<?php

//Connect to db with my params
$db = new mysqli('###', '###', '###', '###');

//Receive today's date
$date = date('Y-m-d');

/*
* Schedule - is what I'm looking for(some text in the table), knu - table's name
* date - table's field with date(format YYYY-MM-DD)
*/

$query = "SELECT schedule FROM knu WHERE `date` = '$date'";
$res = $db->query($query);
$row = $res->fetch_assoc();


//Send message to user
if($message == "a")
{
$date = date('Y-m-d');
$answer = $row;
sendMessage($chatId, $answer);
}


And this code doesn't work. I've tried a lot of times with different ways but still no answer with my bot.

tl;dr What's wrong with my code and how to make it work?

Answer

$row isn't a string, it's an array. You probably want $answer['schedule'] or even just $row['schedule'] allowing you to remove the line $answer = $row;.

Comments