Hello! Here is my php code:

$query = 'SELECT * from infourl'; $result = mysqli_query($con, $query) or die("Invalid Query, " . mysql_error()); while( $row = mysqli_fetch_object($result) ) { $rows['info'] = array($row); } echo json_encode($rows); 

And here is the result:

 { "info":[ { "id":"2", "url":"http:\/\/video2.php", "title":"Two Video", "comment":"Two Comment" } ] } 

But I have 2 records in the database, but only one output. Why ???? Help pliz.

  • So you rewrite the value in $rows['info'] - Dmitry at each iteration of the cycle
  • in another language, I would create a cycle for, and how can I implement here ?? If not difficult, then sample code. - Dmitriy Dev

1 answer 1

in general, you can do it

 $query = 'SELECT * from infourl'; $result = mysqli_query($con, $query); if(!$result) { error_log(mysqli_error()); // не стоит полить запрос и странно зачемвы используете mysql_error? } $rows['info'] = iterator_to_array(mysqli_fetch_object($result)); echo json_encode($rows); 

although it may even be better to use mysqli_fetch_array but I am not familiar with mysqli so sorry. If someone can predict whether mysqli_fetch_array will return a complete array of strings, please write.

  • There is nothing to write braces around each operator. Do you mean this - mysqli_fetch_all? - splash58 4:14
  • @ splash58 coding style and psr1 and psr2 standards - Naumov
  • You are right: Must be enclosed by braces. This is a standardized way to get to the body. Now you need to read, for example, here - gramma.ru , in order to write correctly in Russian :)) - splash58
  • @ splash58 if you know Russian and literacy. Higher than programming and knowledge of mathematics, thinking abstraction is more suitable for you than HBR than CO ... if a person is illiterate and expresses his thoughts in something else, this is not a reason to scam him ... I do not advise you to memorize coding, so that you would The code was written which can be read, I don’t even speak about documenting my code, for you it’s not even necessary to say anything about the documentation, and so everyone will understand. - Naumov
  • I think that this is not a place for this debate. If you want to continue it, I think you can move to the chat. At the same time tell me that in my message was offensive to you - splash58