Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. QNetworkAccessManager with PHP and MYSQL
QtWS25 Last Chance

QNetworkAccessManager with PHP and MYSQL

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
qnetworkaccessmphpmysql
12 Posts 3 Posters 11.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    werter
    wrote on last edited by
    #1

    Hi all Qt'ers,
    I am trying to use QNetworkAccessManager to get data from php Internet page that shows data from MYSQL base. Unfortunately when I use readAll() function for QNetworkReply object I get the whole QByteArray object starting from "<!DOCTYPE html>..." Is there any possibility to get only data from MYSQL base by calling another function or in other way?

    Kind Regards,
    Werter

    1 Reply Last reply
    0
    • Paul ColbyP Offline
      Paul ColbyP Offline
      Paul Colby
      wrote on last edited by
      #2

      Hi @werter,

      I am trying to use QNetworkAccessManager to get data from php Internet page that shows data from MYSQL base. ... Is there any possibility to get only data from MYSQL base by calling another function or in other way?

      Do you have control over the PHP script?

      If yes, then I'd suggest you modify the PHP page to return non-HTML, such as JSON, using something like print json_encode($rows) (I'm happy to help with that, though obviously its not really a Qt thing ;)

      If no, then you'll need to use QString and friends to parse the HTML... in that case, show us a bit more of the HTML, and we can probably help get you started there too :)

      Cheers.

      W 1 Reply Last reply
      0
      • Paul ColbyP Paul Colby

        Hi @werter,

        I am trying to use QNetworkAccessManager to get data from php Internet page that shows data from MYSQL base. ... Is there any possibility to get only data from MYSQL base by calling another function or in other way?

        Do you have control over the PHP script?

        If yes, then I'd suggest you modify the PHP page to return non-HTML, such as JSON, using something like print json_encode($rows) (I'm happy to help with that, though obviously its not really a Qt thing ;)

        If no, then you'll need to use QString and friends to parse the HTML... in that case, show us a bit more of the HTML, and we can probably help get you started there too :)

        Cheers.

        W Offline
        W Offline
        werter
        wrote on last edited by
        #3

        Hi @Paul-Colby ,
        Thank you for your rapid answer. I have an opportunity to write and change PHP script however the server is not mine. I have changed as You suggested and used 'print json_encode($row)' in PHP script but it did not help. The results on the internet page are displayed in JSON style but the result in Qt is the same. Large QByteArray object with 'doctype html' first.

        Cheers.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          What does your server return exactly ? Do you have a REST endpoint that returns the json data generated from your database ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          W 1 Reply Last reply
          0
          • Paul ColbyP Offline
            Paul ColbyP Offline
            Paul Colby
            wrote on last edited by
            #5

            @werter said:

            I have an opportunity to write and change PHP script

            Great :)

            I have changed as You suggested and used 'print json_encode($row)' in PHP script but it did not help. The results on the internet page are displayed in JSON style but the result in Qt is the same. Large QByteArray object with 'doctype html' first.

            Ok, so the first challenge is to get rid of that doc type.

            Are you able to share a complete (cut down) PHP script? You'll probably want to:

            • add an explicit content-type header, and
            • make sure there's nothing printing anything prior to that header being set.

            I'll see if I can knock up a near-trivial example later today, but I'm wondering if the hosting service might be forcibly adding the doctype on your behalf.

            Once the content is being sent a pure JSON, then in your Qt code, you can simply do: QJsonDocument::fromJson(/*QByteArray*/).

            Cheers.

            1 Reply Last reply
            0
            • Paul ColbyP Offline
              Paul ColbyP Offline
              Paul Colby
              wrote on last edited by Paul Colby
              #6

              I'll see if I can knock up a near-trivial example later today

              Here's a minimal PHP example:

              <?php
              $db = mysql_connect ('hostname', 'username', 'password');
              $res = mysql_query('SELECT * FROM mysql.time_zone_name LIMIT 5');
              while ($row = mysql_fetch_assoc($res))
                  $rows[] = $row;
              
              header('Content-Type: application/json');
              print json_encode($rows);
              

              Output using curl, with verbose output to show the headers:

              curl http://testhost/json.php -v
              ...
               HTTP/1.1 200 OK
              < Date: Mon, 18 Apr 2016 23:15:05 GMT
              < Server: Apache
              < Vary: Accept-Encoding
              < Content-Length: 232
              < Connection: close
              < Content-Type: application/json
              Closing connection #0
              [{"Name":"Africa\/Abidjan","Time_zone_id":"1"},{"Name":"Africa\/Accra","Time_zone_id":"2"},{"Name":"Africa\/Addis_Ababa","Time_zone_id":"3"},{"Name":"Africa\/Algiers","Time_zone_id":"4"},{"Name":"Africa\/Asmara","Time_zone_id":"5"}]
              

              I hope that helps.

              Cheers.

              W 1 Reply Last reply
              0
              • SGaistS SGaist

                Hi,

                What does your server return exactly ? Do you have a REST endpoint that returns the json data generated from your database ?

                W Offline
                W Offline
                werter
                wrote on last edited by
                #7

                @SGaist said:

                Hi,

                What does your server return exactly ? Do you have a REST endpoint that returns the json data generated from your database ?

                Sorry, but I do not know how to check it. The answer was displayed like this:
                {"Index":"1","Name":"aaa","Surname":"AAA","Phone":"111111111"}{"Index":"2","Name":"bbb","Surname":"BBB","Phone":"222222222"}

                1 Reply Last reply
                0
                • Paul ColbyP Paul Colby

                  I'll see if I can knock up a near-trivial example later today

                  Here's a minimal PHP example:

                  <?php
                  $db = mysql_connect ('hostname', 'username', 'password');
                  $res = mysql_query('SELECT * FROM mysql.time_zone_name LIMIT 5');
                  while ($row = mysql_fetch_assoc($res))
                      $rows[] = $row;
                  
                  header('Content-Type: application/json');
                  print json_encode($rows);
                  

                  Output using curl, with verbose output to show the headers:

                  curl http://testhost/json.php -v
                  ...
                   HTTP/1.1 200 OK
                  < Date: Mon, 18 Apr 2016 23:15:05 GMT
                  < Server: Apache
                  < Vary: Accept-Encoding
                  < Content-Length: 232
                  < Connection: close
                  < Content-Type: application/json
                  Closing connection #0
                  [{"Name":"Africa\/Abidjan","Time_zone_id":"1"},{"Name":"Africa\/Accra","Time_zone_id":"2"},{"Name":"Africa\/Addis_Ababa","Time_zone_id":"3"},{"Name":"Africa\/Algiers","Time_zone_id":"4"},{"Name":"Africa\/Asmara","Time_zone_id":"5"}]
                  

                  I hope that helps.

                  Cheers.

                  W Offline
                  W Offline
                  werter
                  wrote on last edited by
                  #8

                  @Paul-Colby said:

                  header('Content-Type: application/json');

                  Using this option above in PHP code I get much less QByteArray object:
                  <!DOCTYPE html>
                  <html>
                  <body>

                  {"Index":"1","Name":"aaa","Surname":"AAA","Phone":"111111111"}{"Index":"2","Name":"bbb","Surname":"BBB","Phone":"222222222"}
                  </body>
                  </html>

                  However I would like to have an opportunity for managing my database on my page: for example display the results for specified options. I did this as an example - displayed all the results from base on my page. Shall I put this piece of php code in any other file (not index.php like now) to do so?

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by SGaist
                    #9

                    If you don't want to have several backends to access your data, you should separate the data source(s) and the rendering part. Think model/view, write one/several web service(s) to access your database and use that to render your website. You can then use the same service from your application.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    W 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      If you don't want to have several backends to access your data, you should separate the data source(s) and the rendering part. Think model/view, write one/several web service(s) to access your database and use that to render your website. You can then use the same service from your application.

                      W Offline
                      W Offline
                      werter
                      wrote on last edited by
                      #10

                      @SGaist said:

                      If you don't want to have several backends to access your data, you should separate the data source(s) and the rendering part. Think model/view, write one/several web service(s) to access your database and use that do render your website. You can then use the same service from your application.

                      Is something I can follow, Internet page tutorial or yt video?

                      @Paul-Colby,
                      And if I get this whole QByteArray can you point out something more than "QString and friends to parse HTML"?

                      I do not have large pression to use QNetworkAccessManager. Maybe I could invoke an Android method from Qt? Would not it be easier? I wonder about it because I would like to save phone number from Internet database on my smartphone.

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        It's not the only way to do it but AngularJS shows the idea pretty well.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • W Offline
                          W Offline
                          werter
                          wrote on last edited by
                          #12

                          Thank you. I take a look on it.

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved