0

I'm trying to use a php script to query a mysql database, make a xml file with the data, then return it to be accessed in the c++ app. I will then be trying to edit the data returned in c++, like printing or truncating it. Right now all I have in my main function in c++ is

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    cout << system("C:/xampp/php/php E:/phpTest.php");
    return 0;
}

my php script includes...

phpTest.pnp:

<?php
require_once("mysqli.php");
$connector = new Mysqli_connector();
$xmlTest = $connector->selectTest();
return $xmlTest;
?>

and mysqli.php:

<?php

class Mysqli_connector {
        var $conn;
        function __construct($db=array()) {
        $default = array(
            'host' => 'hostname',
            'user' => 'username',
            'pass' => 'password',
            'db'   => 'database'
            );

        $db = array_merge($default,$db);
        $this->conn = mysqli_connect($db['host'],$db['user'],$db['pass'],$db['db']) 
            or die("Error connecting to database");
    }

    function __destruct() {
        mysqli_close($this->conn);  
    }

    function selectTest(){
        $res = mysqli_query($this->conn,"select * from loginTest") or die("Error querying");
        $xml_output = "<?xml version=\"1.0\"?>\n";
        $xml_output .= "<Data>" . "\n";
        $xml_output .= "field1" ."\t". "field2" ."\t". "field3" ."\t". "field4" ."\t". "field5";
        $xml_output .= "\n";

        for($x = 0; $x < mysqli_num_rows($res); $x++)
        {
            $row = mysqli_fetch_assoc($res);
            $xml_output .= $row['field1'] . "\t";
            $xml_output .= $row['field2'] . "\t";
            $xml_output .= $row['field3'] . "\t";
            $xml_output .= $row['field4'] . "\t";
            $xml_output .= $row['field5'] . "\n";
        }
        return $xml_output;
    }
}
?>

Using return does seem to work when I return from mysqli.php, but then I cannot send it to the c++ app using the same return statement. Does anyone know the proper way to do that?

Aziz Shaikh
  • 16,245
  • 11
  • 62
  • 79
user2761933
  • 179
  • 1
  • 4
  • 15
  • For what it's worth, I know it is creating the XML correctly because if I change the `return` statement to an `echo` it does print what I'd expect. – user2761933 Jan 06 '14 at 20:12

1 Answers1

0

Exactly as your comment above says, change the return to an echo. The top-level of any PHP script (e.g. outside of function calls), is NOT a function, and has nowhere to return to. In other words, there's nothing "higher" to capture the value you return, so the script simply exits.

Changing to echo forces your XML to be actually output, which will be captured by your c++ app.

e.g. If your script was:

<?php

echo 'foo';
return 'bar';

Your C++ app would see foo come back, not foobar - the return value is lost because there's no function call to have returned FROM.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • I apologize, I should have been more clear. I need to get the XML as a variable so I can mess with it, not just print it out. So for instance I could then use c++ to get just field1 for each object, rather than print the entire thing. Is that doable with XML or am I using the wrong format? – user2761933 Jan 06 '14 at 20:23
  • well, I have no idea what your `selectTest()` function is actually doing, but if it's returning xml, then `$xmlTest` will contain that xml. – Marc B Jan 06 '14 at 20:25
  • Right, but how do I get that into my c++ app to edit the contents of `$xmlTest`? – user2761933 Jan 06 '14 at 20:28
  • Don't use system. it doesn't capture the output, only the exit code of the program you ran. See: http://stackoverflow.com/questions/125828/capturing-stdout-from-a-system-command-optimally – Marc B Jan 06 '14 at 20:32