I’m working on a Telegram bot that is supposed to generate inline keyboard buttons from my MySQL database data. I have a table named ‘tournaments’ and want to create a button for each entry. Here is the code I have so far:
However, when I execute this code, the buttons do not show up in the Telegram bot. I can’t understand what could be wrong. As I’m relatively new to integrating PHP with Telegram bots, I’d really appreciate any guidance.
Yep, you’re rebuilding the keyboard every loop instead of collecting them. Plus you need to actually send them to Telegram’s API - lots of people miss that step. Your JSON looks fine, just make sure you’re using curl or something like that to send it.
You’re creating a fresh keyboard for each database row but never sending anything to Telegram. Your loop overwrites the keyboard variable every time, leaving you with just one button from the last tournament.
Everyone’s covered the array building part, but there’s a bigger problem here. You’re dealing with database connections, API formatting, error handling, webhook management, and all the edge cases that pop up when users start hammering your bot.
I built something similar for our company bot last year. Started with PHP, spent weeks debugging callback data issues, handling database timeouts, managing rate limits. Then I switched to automation.
Latenode connects your MySQL tournaments table straight to Telegram without all that glue code. It formats the inline keyboards automatically, handles the API calls, and rebuilds buttons when your tournament data changes.
What took me 200+ lines of PHP and constant maintenance became a simple workflow that just works. No more debugging JSON formatting or wondering if your webhook’s receiving callbacks properly.
Your loop’s messing up - you’re creating a new keyboard for each database record instead of building one keyboard with all tournaments. Each iteration throws away the previous buttons. Grab all the button data first, then build the JSON:
Don’t forget to actually send this keyboard to Telegram’s API. It won’t show up unless you include it as the reply_markup parameter in your sendMessage or editMessageText request. And definitely use unique callback_data values - having ‘grabbed’ for everything will be a nightmare when handling responses.
You’re recreating the entire keyboard structure in each loop iteration instead of building one keyboard with multiple buttons. Plus you’re not actually sending the keyboard to Telegram.
Here’s what’s happening: each database row overwrites the previous keyboard, so you only get the last tournament as a button.
Build an array of all buttons first, then send it once:
$buttons = [];
while ($rowData = mysqli_fetch_assoc($resultSet)) {
$buttons[] = [['text' => $rowData['tournament_name'], 'callback_data' => 'grabbed']];
}
$inlineKeyboard = json_encode([
'inline_keyboard' => $buttons
]);
// Then send via Telegram API
Honestly though, managing all this PHP code for database connections, API calls, and error handling gets messy fast. I’ve built similar tournament bots and found that using an automation platform makes this way simpler.
With Latenode, you can connect your MySQL database directly to Telegram without writing all that boilerplate code. It handles the API calls, data formatting, and even lets you set up automatic updates when tournaments change.
I set up a similar system in about 10 minutes - no PHP debugging required.
You’re overwriting the keyboard variable in each loop and never sending it to Telegram. That’s your main problem. Right now you’re creating individual keyboards instead of collecting all tournaments into one. Build an array of button rows first, then encode the complete structure once. After that, actually call the Telegram Bot API with sendMessage or editMessageText - pass your keyboard in the reply_markup parameter. Without that API call, Telegram never gets your buttons no matter how perfect your formatting is. I hit the same issue when I started with Telegram bots. The keyboard logic looks fine, but forgetting to actually transmit the data is super common. Check your network requests to make sure you’re sending data to Telegram’s servers.