0

I am using PHP-CPP to create a php extension which contains a function that will parse a table from mysql.

extension.cpp

#include <phpcpp.h>
#include <iostream>
#include <mysql.h>

Php::Value  ss_parse_table(Php::Parameters &params)
{ 
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
MYSQL_FIELD *field;

/* INITIALIZE CONNECTION HANDLER, DO NOT CHANGE */
conn = mysql_init (NULL);


// @param orders: host,username,password,database,
mysql_real_connect (conn, params[0], params[1], params[2], params[3], 3306, NULL, 0);
/* show tables in the database (test for errors also) */


mysql_query(conn,("SELECT * FROM table");
res = mysql_store_result(conn);


// get the number of the columns
int num_fields = mysql_num_fields(res);

Php::Value array;
Php::Value rows;
int i = 0;

while((field = mysql_fetch_field(res))){
    array[i] = field->name;
    i++;
}

int x = 0;
while ((row = mysql_fetch_row(res)))
{

    for (int z=0;z<num_fields;z++) 
    {
        std::string fieldname = array[z];
        rows[x][fieldname] = row[z];
    }

    x++;
}

// DON'T FORGET TO CLEAN RESULT AFTER YOU DON'T NEED IT 
// ANYMORE
if(res != NULL)
   mysql_free_result(res);

/* disconnect from server */
mysql_close (conn);

return rows;

}

extern "C" {


  PHPCPP_EXPORT void *get_module() 
  {
    \
    static Php::Extension extension("extension", "1.0");
    extension.add("ss_parse_table",ss_parse_table);
    // @todo    add your own functions, classes, namespaces to the extension

    // return the extension
    return extension;
  }
}

and in compiling I edited the MakeFile and appended mysql_config --cflags --libs in the compiler command in order to load mysql so it will look like g++ -shared -o extension.so extension.o -lphpcpp mysql_config --cflags --libs the compilation works fine.

I tried testing if the compilation really worked by using the defined function (ss_parse_table) from the extension. So I created a php file:

test.php

<?php
  print_r(ss_parse_table( "localhost", "dbuser","dbpassword","database"));
 ?>

and from the CLI when I run the command 'php /mnt/test/index.php' it works fine. The command will output array returned by the defined extension function (ss_parse_table), something like this

Array
(
  [0] => Array
  (
    [id] => 'erd'
  )
)

but when I browse it from the browser the http fails

'No data received' (chrome error) or 'The connection was reset' (firefox error)

1 Answer 1

0

I had exactly the same problem. I solved this by adding the directory where my libmysqlclient.so file is located to the system PATH:

PATH=$PATH:/usr/lib64

and then, restarting httpd service:

systemctl restart httpd.service

After that, everything worked fine.

I hope it helps

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.