Home php PHP SQLITE3 multiple sample

PHP SQLITE3 multiple sample

Author

Date

Category

There was a strange problem when requesting:

$ result = $ this- & gt; Conn- & gt; Query ("Select Student_id AS ID, AVG (Mark) AS AVERAGE FROM MARKS GROUP by Student_id Order by Avg (Mark) Desc Limit 15;") - & gt; fetcharray (sqlite3_assoc);

in $ result I get array ([id] = & gt; 1 [average] = & gt; 3.1739130434783) , although when requested directly
(through the SQLite3.exe utility)
I get a full list:

SQLite Version 3.31.1 2020-01-27 19:55:54
ENTER ".HELP" for usage hints.
SQLITE & GT; SELECT STUDENT_ID AS ID, AVG (MARK) AS AVERAGE FROM MARKS GROUP by Student_id Order by Avg (Mark) DESC LIMIT 15;
1 | 3.17391304347826.
2 | 3.0
7 | 1.0
6 | 1.0.
5 | 1.0
4 | 1.0.
3 | 1.0.
SQLITE & GT;

I do not know what the problem is.

Updates

found, it seems, the decision, but I absolutely do not like it.

while ($ row = $ result- & gt; fetcharray ()) {
  // Do Something Wit $ Row
  Print_R ($ Row);
}

Is there a way to one line?


Answer 1, Authority 100%

In general, the question of those for which the answer is 5 seconds in the documentation, but according to the Stack Overflow rules, it is still necessary to give an answer.

As can be seen from the documentation, ready method To get a row array in php_sqlite no

That is, we have two ways

First – instead of PHP_SQLite to use PDO. There this method is .
The second is to remember that we are programmers, and not just fold the site from ready-made cubes, and write such a function yourself

function sqlite_array ($ result, $ mode = sqlite3_assoc) {
  $ data = [];
  While ($ Row = $ Result- & GT; fetcharray ($ mode)) {
    $ Data [] = $ row;
  }
  Return $ Data;
}

and then use it non-renewable to get “in one line”, but at the same time not stretching code as a rubber band from panties

$ sql = "select student_id as id, avg (mark) AS AVERAGE FROM MARKS
    Group by Student_id Order by Avg (Mark) Desc Limit 15; "
$ result = sqlite_array ($ this- & gt; Conn- & gt; query ($ SQL));

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions