PHP Telegram bot fails when connecting to MySQL database

I’m building a Telegram bot for my cafe using PHP on Heroku with a MySQL database hosted on another server. The bot should handle table bookings and show daily specials. However, whenever I add MySQL connection code, the entire bot stops working. Without the database part, everything runs perfectly.

Here’s my current code:

<?php
header('Content-Type: application/json');
$input = file_get_contents("php://input");
$data = json_decode($input, true);

$connection = mysqli_connect('server', 'user', 'pass', 'database');
$sql = "SELECT * FROM daily_specials ORDER BY position ASC";
$result = mysqli_query($connection, $sql);
$items = array();
while ($record = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
  $items[] = array(
    'category' => $record['CATEGORY'],
    'dish_name' => $record['DISH']
  );
}
$daily_menu = json_encode($items);

$msg = isset($data['message']) ? $data['message'] : "";
$msg_id = isset($msg['message_id']) ? $msg['message_id'] : "";
$chat_id = isset($msg['chat']['id']) ? $msg['chat']['id'] : "";
$first_name = isset($msg['chat']['first_name']) ? $msg['chat']['first_name'] : "";
$user_text = isset($msg['text']) ? $msg['text'] : "";
$user_text = strtolower(trim($user_text));
$today = date('Y-m-d');
$reply = '';

if(strpos($user_text, "/start") === 0 || $user_text=="hello") {
    $reply = "Welcome $first_name! Type 'booking' to reserve a table or 'specials' to see today's menu!";
}
elseif($user_text=="booking") {
    $reply = "How many people for $today?";
}
elseif($user_text=="specials") {
    $reply = "Today's specials: $daily_menu";
}
else {
    $reply = "Invalid command!";
}

$params = array('chat_id' => $chat_id, "text" => $reply);
$params["method"] = "sendMessage";
echo json_encode($params);
?>

What could be causing this issue? Can MySQL be used with Telegram bots or is there a compatibility problem?

check your heroku logs first - likely a connection timeout or wrong creds. mysql works fine with telegram bots, i’v used it tons of times. add error checking after mysqli_connect() to see what’s actually failing.

Your code’s probably hitting the MySQL query on every webhook call, even when there’s nothing to process. If the database connection fails or takes too long, Telegram times out waiting for a response. You need error handling and should only query the database when you actually need to. Wrap your mysqli_connect in a conditional check and add mysqli_connect_error() to see what’s breaking. Also, move that daily specials query inside the elseif block for the ‘specials’ command instead of running it every single request - it’ll cut down on unnecessary database load and prevent those timeout issues.

Your database connection is probably blocking the webhook response. Telegram wants a response in 5-10 seconds, but if your MySQL server’s on another host, the connection + query time might blow past that limit. I ran into this exact issue with a bot using external databases. Add mysqli_connect_error() right after your connection line to see if it’s actually connecting. I’d also switch to PDO with try-catch blocks instead of mysqli - way better error handling. What really helped me was connection pooling or caching the daily specials locally instead of hitting the database every single request. Also double-check your MySQL credentials for the external server - make sure the host, port are right and that the database allows connections from Heroku’s IP ranges.