Hello, dear!

I had a problem, the fact is that I am making a small application, and at this stage I have to set the output of user data by their id. For this, I want to use the users.get method, and pass all user data in one request. But the problem is that you need to somehow pack all the data from the array into a string and for that there is an implode function, but the fact is that when using mysql_fetch_assoc, I only get one cell of all the columns, not all at once.

How can this problem be solved? I admit that overwriting an array into another. But maybe there is a function? PS About mysqli_fetch_all read, but it did not work with it + it is turned off on many hosts. The code itself:

<?php require_once('db.php'); $sql_connection = dbConnect(); $_REQUEST['viewer_id'] = 2; function getUsers($user_id, $sql_connection){ $sql_query = "SELECT * FROM `catch_up` WHERE `owner_id` LIKE ".$user_id." "; $sql_response = mysql_query($sql_query); return $sql_response; } function showUsers($sql_response,$sql_connection){ while ($row = mysql_fetch_assoc($sql_response)){ // Тут должен быть вывод данных } } $sql_response = getUsers($_REQUEST['viewer_id'],$sql_connection); showUsers($sql_response,$sql_connection); ?> 

UPD:

While I tried:

 $data = array(); while($row = mysql_fetch_assoc($sql_response)) { $data[] = $row; return $data; } 

+ then var_dump array

But all the same one output, instead of two: (

UPD 2: I am a rakan, I put return into a loop. I'll fix it now. UPD 3:

We have already decided at least somehow the first problem, now the problems with Implode.

When implode, for some reason, it does not glue everything into a string, but returns with var_dump: string (11) "Array, Array" The code itself (screenshot): pp.vk.me/c630925/v630925489/448b7/J6bHjj_eNmY.jpg What is wrong? The data array itself contains the following form: array (2) {[0] => array (1) {["v_id"] => string (1) "1"} 1 => array (1) {["v_id" ] => string (1) "2"}}

  • Add the result to an array and work with it while ($rows[] = mysql_fetch_assoc($sql_response)); print_r($rows); while ($rows[] = mysql_fetch_assoc($sql_response)); print_r($rows); Well, note that the methods are obsolete. - Bookin
  • @Bookin, thank you, kind man. This option will be much more optimized! About the fact that it is outdated, I read, but I could not find a suitable alternative :( - Felix
  • I wonder what you will be doing in a couple of years, when normal hosting companies switch to a more recent php in a cat, there are no mysql_ * functions at all. Choose hosting services where PDO is already available and use PDO functions, for example fetchAll. - Mike
  • @Mike, thanks for the advice, I know about it and will try to add knowledge in the near future, because I myself understand that these are all outdated methods. Just, just learning. - Felix
  • And by the way, I wonder why you like, id is a number in theory, why look for it as a string. And yes, never substitute variables directly in the text. 50% of sites hack precisely because of this code. Use bindParam - Mike

1 answer 1

  1. instead of mysql_query you need to use PDO
  2. json_encode should be used instead of implode

so we write.

 function getUsers($user_id, $pdo){ $stmt = $pdo->prepare("SELECT * FROM `catch_up` WHERE `owner_id` = ?"); $stmt->execute([$user_id]); return $stmt->fetchAll(); } echo json_encode(getUsers($_REQUEST['viewer_id'], $pdo)); 

How to connect to PDO