M
M
Maxim Kurganov2020-09-09 12:19:04
PHP
Maxim Kurganov, 2020-09-09 12:19:04

How to correctly display information from an array?

Hello.
I apologize in advance for stupid questions, I'm studying php for the fourth day.) I'm trying to get the Telegram id of all bot visitors .. that's what happened

ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);

$bd = new PDO('mysql:dbname=testbd;host=localhost', 'testbduser', 'G9yR37', array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));

function user() {
  global $bd;
  $stmt = $bd->query("SELECT `tg_id` FROM `user`");
  $data = $stmt->fetchAll();
  return $data;
 }
$datas = user();

foreach ($datas as $value) {
echo "<b>$value</b><br>";
}

//$message = "Test rassilki";
//sendTg($message);  
  
//    function sendTg($message){
//        $tokken = "111081012:AAF5DzPQ4bjxO94BWvGM_JDiey_fqgiKE"; // токен бота
//		$filename = "https://api.telegram.org/bot".$tokken."/sendMessage?chat_id=".$value."&text=".urlencode($message)."&parse_mode=html";
//		file_get_contents($filename);
//    }
?>

Gives an error message
Notice: Array to string conversion in /var/www/www-root/data/www/domain.com/1.php on line 19
Array

Through print_r , the array is displayed on the screen. Do I do it all?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FanatPHP, 2020-09-09
@givi_m

In this particular case,
$data = $stmt->fetchAll(PDO::FETCH_COLUMN);
but for the future, try to understand what nested arrays are .
in particular, compare the print_r output of two arrays

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question