0

I'm making a Telegram bot in PHP. I have connected a Mysql database and I need to print some results in a unique message. But when I start the command it print every results in some messages. How can I print all results in a unique message?

This is the code:

if($data == 'rawliberi'){
  $thequery = "SELECT * FROM uwgliberi WHERE roster='OCW'";
  $thequeryup = mysqli_query($conn, $thequery);
  while($row = mysqli_fetch_assoc($thequeryup)){
       $Alex -> Request('sendMessage', ['chat_id' => $chat_id, 'text' => $row['wrestlername'], 'parse_mode' => 'HTML']);
  }
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Cuaddus
  • 3
  • 2
  • your databse structure is unknown so it is not piossble to give you an exact answer. see https://stackoverflow.com/a/14456661/5193536 how you can access the columns and add them to your messagetext as needed – nbk Jul 20 '20 at 19:15

1 Answers1

0

You are sending a message for each result in the query because the while loop is executed for all the rows. If I understand correctly you want to send a message with all results of the query. I advise you to use PDO to connect and fetch/insert data from/to a database because is more secure.

Try this:

<?php
$pdo = new PDO("mysql:host=localhost;dbname=DATABASE_NAME", "USERNAME", "PASSWORD");


if($data == 'rawliberi'){
  $allResults = "";

  $stmt = $pdo->prepare("SELECT * FROM uwgliberi WHERE roster=:roster"); // Prepare the query
  $stmt->execute(['roster' => "OCW"]); // Replace parameters starting with ":" with the given value (e.g. replace ":roster" with "OCW") and execute the query
  $results = $stmt->fetchAll(\PDO::FETCH_ASSOC); // Insert results in the $results variable
/*
Result's value example
[
  {
    "id": "1",
    "wrestlername": "Nicola",
    "roster": "OCW"
  },
  {
    "id": "2",
    "wrestlername": "Mauro",
    "roster": "OCW"
  },
  {
    "id": "3",
    "wrestlername": "Don Gino",
    "roster": "OCW"
  }
]

*/


  for ($i = 0; $i < count($results); $i++){ // Execute this loop for each result in $results. The count() function return the count of the results

    $allResults .= $results[$i]['wrestlername'] . "\n"; // Add the wrestlername to the allResults variable and go to a new line (\n) so the names won't be "NicolaMauroDon Gino"
  }
  $Alex->Request('sendMessage', ['chat_id' => $chat_id, 'text' => $allResults, 'parse_mode' => 'HTML']);
}

GioIacca9
  • 406
  • 4
  • 8
  • Hi Gio! By name I suppose you're italian like me! This code doesn't works because it prints only a database value while I gotta print 20 values in a unic message. Please, help me. – Cuaddus Jul 20 '20 at 18:55
  • Yes I'm italian :) Anyway, I just edited my answer, try that. Also, if you want, try [my php base](https://github.com/GioIacca9/PHPTGBot) to create a Telegram Bot easily – GioIacca9 Jul 21 '20 at 16:53
  • Hi @Cuaddus if this or any answer has solved your question please consider [accepting it](https://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – GioIacca9 Jul 23 '20 at 16:48