OS : win7 64bits
Qt : 5.2.1
WAMP :

Originally Posted by
anda_skoa
No, this is one option.
Additional to the web service approach already mentioned there is also the option of splitting the Qt application in frontend (client) and backend (server).
Or connecting through a VPN, an SSH tunnel, etc,
Cheers,
_
Recently I give this solution a try
read_data.php
<?php
$con = mysqli_connect($_POST['db_server'], $_POST['db_user'],
$_POST['db_password'], $_POST['db_database']) or die(mysql_error());
if (mysqli_connect_errno()){
mysqli_close($con);
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, $_POST['query_message']);
if (mysqli_num_rows($result) > 0) {
$response["results"] = array();
$field = array();
while ($property = mysqli_fetch_field($result)) {
$field[] = $property->name;
//echo $property->name .";";
}
//echo(count($field));
while ($row = mysqli_fetch_array($result, MYSQLI_NUM)) {
$table_data = array();
//echo(count($row) .";");
for ($i = 0; $i < count($row); ++$i) {
$table_data[$field[$i]] = $row[$i];
}
// push single temp into final response array
array_push($response["results"], $table_data);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
}else{
// no products found
$response["success"] = 0;
$response["message"] = mysqli_error($con);
// echo no users JSON
echo json_encode($response);
}
?>
<?php
$con = mysqli_connect($_POST['db_server'], $_POST['db_user'],
$_POST['db_password'], $_POST['db_database']) or die(mysql_error());
if (mysqli_connect_errno()){
mysqli_close($con);
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, $_POST['query_message']);
if (mysqli_num_rows($result) > 0) {
$response["results"] = array();
$field = array();
while ($property = mysqli_fetch_field($result)) {
$field[] = $property->name;
//echo $property->name .";";
}
//echo(count($field));
while ($row = mysqli_fetch_array($result, MYSQLI_NUM)) {
$table_data = array();
//echo(count($row) .";");
for ($i = 0; $i < count($row); ++$i) {
$table_data[$field[$i]] = $row[$i];
}
// push single temp into final response array
array_push($response["results"], $table_data);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
}else{
// no products found
$response["success"] = 0;
$response["message"] = mysqli_error($con);
// echo no users JSON
echo json_encode($response);
}
?>
To copy to clipboard, switch view to plain text mode
main.cpp
#include "MainWindow.hpp"
#include <QApplication>
#include <QDebug>
#include <QByteArray>
#include <QEventLoop>
#include <QNetworkReply>
#include <QTextCodec>
#include <QUrlQuery>
int main(int argc, char *argv[])
{
QString const &DB_SERVER
= "1xx.1xx.1xx.1xx";
QNetworkRequest request("http://" + "DB_SERVER" + "/bridge/read_data.php");
request.setHeader(QNetworkRequest::ContentTypeHeader,
"application/x-www-form-urlencoded");
QUrlQuery query;
query.addQueryItem("db_database", "student_sms");
query.addQueryItem("db_password", "1234");
query.addQueryItem("db_server", DB_SERVER);
query.addQueryItem("db_user", "root");
query.addQueryItem("query_message", "select * from student_sms_basic");
QNetworkAccessManager network_manager;
auto network_reply = network_manager.post(request,
query.query().toUtf8());
connect(network_reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
if(network_reply->error() == QNetworkReply::NoError){
raw_data = network_reply->readAll();
}else{
qDebug()<<network_reply->errorString();
}
qDebug()<<QTextCodec::codecForHtml(raw_data)->toUnicode(raw_data);
}
#include "MainWindow.hpp"
#include <QApplication>
#include <QDebug>
#include <QByteArray>
#include <QEventLoop>
#include <QNetworkReply>
#include <QTextCodec>
#include <QUrlQuery>
int main(int argc, char *argv[])
{
QString const &DB_SERVER = "1xx.1xx.1xx.1xx";
QNetworkRequest request("http://" + "DB_SERVER" + "/bridge/read_data.php");
request.setHeader(QNetworkRequest::ContentTypeHeader,
"application/x-www-form-urlencoded");
QUrlQuery query;
query.addQueryItem("db_database", "student_sms");
query.addQueryItem("db_password", "1234");
query.addQueryItem("db_server", DB_SERVER);
query.addQueryItem("db_user", "root");
query.addQueryItem("query_message", "select * from student_sms_basic");
QNetworkAccessManager network_manager;
auto network_reply = network_manager.post(request,
query.query().toUtf8());
QEventLoop loop;
connect(network_reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
QByteArray raw_data;
if(network_reply->error() == QNetworkReply::NoError){
raw_data = network_reply->readAll();
}else{
qDebug()<<network_reply->errorString();
}
qDebug()<<QTextCodec::codecForHtml(raw_data)->toUnicode(raw_data);
}
To copy to clipboard, switch view to plain text mode
I can query the data from local(server == 127.0.0.1) but can't query the data if I try
to access the server remotely(access deny). What kind of error I make?
Bookmarks