Jump to page: 1 2
Thread overview
PHP to D Conversion
Oct 18, 2019
Vino
Oct 18, 2019
Jacob Carlborg
Oct 18, 2019
Vino
Oct 18, 2019
Vino
Oct 18, 2019
Vino
Oct 18, 2019
Andre Pany
Oct 19, 2019
Vino
Oct 19, 2019
Boris Carvajal
Oct 21, 2019
Vino
Oct 21, 2019
Andre Pany
Oct 18, 2019
Aldo
Oct 21, 2019
zoujiaqing
Oct 21, 2019
Andre Pany
Oct 22, 2019
zoujiaqing
Oct 22, 2019
zoujiaqing
October 18, 2019
Hi All,

  Request your help in converting a PHP code to D equivalent code

PHP Code:
class avmtest {
        private $con;

        function __construct() {
                global $config;
                $this->con = new mysqli(test.srv.com:3910, testusr, xxxx#, test);
                if($this->con->connect_errno) { die("Connection Failed.\n"); }
        }

        function getHostname() {
                $qdata = $this->con->prepare("SELECT host_name FROM hosts_coll");
                $qdata->execute();
                $qdata->bind_result($host);
                while($qdata->fetch()) {
                        $data[] = array("HostName" => $host);
                }
                $sdata->close();
                return $data;
        }
}

D Code:
module avm.test;

import mysql;
import std.array : array;
import std.conv;
import std.variant;

class avmtest
{
	private conn;
	
	auto avmconnect()
	{
	auto connectionStr = "host=test.srv.com;port=3910;user=testusr;pwd=xxxx#;db=test";
	Connection conn = new Connection(connectionStr);
	scope(exit) conn.close();
    }

	auto getHostname()
	{
	  ResultRange qdata = conn.query("SELECT host_name FROM `hosts_coll`");
	  Row row = qdata.front;
	  Variant h = row[0];
	  qdata.close();
	  return h.to!string;
	}
}

Error: Error: no identifier for declarator conn

From,
Vino.B
October 18, 2019
On Friday, 18 October 2019 at 06:22:33 UTC, Vino wrote:
> Hi All,
>
>   Request your help in converting a PHP code to D equivalent code
>
> PHP Code:
> class avmtest {
>         private $con;
>
>         function __construct() {
>                 global $config;
>                 $this->con = new mysqli(test.srv.com:3910, testusr, xxxx#, test);
>                 if($this->con->connect_errno) { die("Connection Failed.\n"); }
>         }
>
>         function getHostname() {
>                 $qdata = $this->con->prepare("SELECT host_name FROM hosts_coll");
>                 $qdata->execute();
>                 $qdata->bind_result($host);
>                 while($qdata->fetch()) {
>                         $data[] = array("HostName" => $host);
>                 }
>                 $sdata->close();
>                 return $data;
>         }
> }
>
> D Code:
> module avm.test;
>
> import mysql;
> import std.array : array;
> import std.conv;
> import std.variant;
>
> class avmtest
> {
> 	private conn;
> 	
> 	auto avmconnect()
> 	{
> 	auto connectionStr = "host=test.srv.com;port=3910;user=testusr;pwd=xxxx#;db=test";
> 	Connection conn = new Connection(connectionStr);
> 	scope(exit) conn.close();
>     }
>
> 	auto getHostname()
> 	{
> 	  ResultRange qdata = conn.query("SELECT host_name FROM `hosts_coll`");
> 	  Row row = qdata.front;
> 	  Variant h = row[0];
> 	  qdata.close();
> 	  return h.to!string;
> 	}
> }
>
> Error: Error: no identifier for declarator conn

The instance variable in the D code, `conn`,  doesn't have a type. I guess the type should be `Connection`. In the D version of `avmconnect` you're declaring a local variable named `conn` instead of referring to the instance variable.

--
/Jacob Carlborg


October 18, 2019
On Friday, 18 October 2019 at 06:22:33 UTC, Vino wrote:
> class avmtest
> {
> 	private conn;
> 

You need to specify the type of conn. private Connection conn;

> 	auto avmconnect()
> 	{
> 	auto connectionStr = "host=test.srv.com;port=3910;user=testusr;pwd=xxxx#;db=test";
> 	Connection conn = new Connection(connectionStr);
> 	scope(exit) conn.close();
>     }

You are using a new connection here, maybe you want to use this->conn instead of Connection conn;

By the way you are closing it everytime with scope(exit).
October 18, 2019
On Friday, 18 October 2019 at 08:54:40 UTC, Jacob Carlborg wrote:
> On Friday, 18 October 2019 at 06:22:33 UTC, Vino wrote:
>> [...]
>
> The instance variable in the D code, `conn`,  doesn't have a type. I guess the type should be `Connection`. In the D version of `avmconnect` you're declaring a local variable named `conn` instead of referring to the instance variable.
>
> --
> /Jacob Carlborg

Hi Jacob,

 Thank you, I have made the below changes, and rebuilt the application, now the application complied without any error, but when i try to access the URL from webpage it errors out with  "127.0.0.1 refused to connect"

private Connection conn; //added

scope(exit) conn.close(); //removed

From,
Vino.B
October 18, 2019
On Friday, 18 October 2019 at 09:11:18 UTC, Vino wrote:
> On Friday, 18 October 2019 at 08:54:40 UTC, Jacob Carlborg wrote:
>> On Friday, 18 October 2019 at 06:22:33 UTC, Vino wrote:
>>> [...]
>>
>> The instance variable in the D code, `conn`,  doesn't have a type. I guess the type should be `Connection`. In the D version of `avmconnect` you're declaring a local variable named `conn` instead of referring to the instance variable.
>>
>> --
>> /Jacob Carlborg
>
> Hi Jacob,
>
>  Thank you, I have made the below changes, and rebuilt the application, now the application complied without any error, but when i try to access the URL from webpage it errors out with  "127.0.0.1 refused to connect"
>
> private Connection conn; //added
>
> scope(exit) conn.close(); //removed
>
> From,
> Vino.B

App.d

import vibe.vibe;
import avm.test;

void main()
{
	auto settings = new HTTPServerSettings;
	settings.port = 8080;
	settings.bindAddresses = ["127.0.0.1"];
	listenHTTP(settings, &hello);

	logInfo("Please open http://127.0.0.1:8080/ in your browser.");
	runApplication();
}

void hello(HTTPServerRequest req, HTTPServerResponse res)
{
    test t = new avmtest();
    res.writeBody(t.getHostname);
}
October 18, 2019
On Friday, 18 October 2019 at 09:17:24 UTC, Vino wrote:
> On Friday, 18 October 2019 at 09:11:18 UTC, Vino wrote:
>> [...]
>
> App.d
>
> import vibe.vibe;
> import avm.test;
>
> void main()
> {
> 	auto settings = new HTTPServerSettings;
> 	settings.port = 8080;
> 	settings.bindAddresses = ["127.0.0.1"];
> 	listenHTTP(settings, &hello);
>
> 	logInfo("Please open http://127.0.0.1:8080/ in your browser.");
> 	runApplication();
> }
>
> void hello(HTTPServerRequest req, HTTPServerResponse res)
> {
>     test t = new avmtest();
>     res.writeBody(t.getHostname);
> }

And now getting the error : Program exited with code -1073741819
October 18, 2019
On Friday, 18 October 2019 at 09:21:46 UTC, Vino wrote:
> On Friday, 18 October 2019 at 09:17:24 UTC, Vino wrote:
>> On Friday, 18 October 2019 at 09:11:18 UTC, Vino wrote:
>>> [...]
>>
>> App.d
>>
>> import vibe.vibe;
>> import avm.test;
>>
>> void main()
>> {
>> 	auto settings = new HTTPServerSettings;
>> 	settings.port = 8080;
>> 	settings.bindAddresses = ["127.0.0.1"];
>> 	listenHTTP(settings, &hello);
>>
>> 	logInfo("Please open http://127.0.0.1:8080/ in your browser.");
>> 	runApplication();
>> }
>>
>> void hello(HTTPServerRequest req, HTTPServerResponse res)
>> {
>>     test t = new avmtest();
>>     res.writeBody(t.getHostname);
>> }
>
> And now getting the error : Program exited with code -1073741819

Hi,

Maybe port 8080 is blocked, because there is an instance of your application running on the background. Just check by changing the port in the source code.

Kind regards
Andre
October 19, 2019
On Friday, 18 October 2019 at 14:56:05 UTC, Andre Pany wrote:
> On Friday, 18 October 2019 at 09:21:46 UTC, Vino wrote:
>> On Friday, 18 October 2019 at 09:17:24 UTC, Vino wrote:
>>> [...]
>>
>> And now getting the error : Program exited with code -1073741819
>
> Hi,
>
> Maybe port 8080 is blocked, because there is an instance of your application running on the background. Just check by changing the port in the source code.
>
> Kind regards
> Andre

Hi Andre,

  Tried with different ports still no luck getting this error : Program exited with code -1073741819.

From,
Vino.B
October 19, 2019
On Saturday, 19 October 2019 at 19:08:45 UTC, Vino wrote:
> On Friday, 18 October 2019 at 14:56:05 UTC, Andre Pany wrote:
>> On Friday, 18 October 2019 at 09:21:46 UTC, Vino wrote:
>>> On Friday, 18 October 2019 at 09:17:24 UTC, Vino wrote:
>>>> [...]
>>>
>>> And now getting the error : Program exited with code -1073741819
>>
>> Hi,
>>
>> Maybe port 8080 is blocked, because there is an instance of your application running on the background. Just check by changing the port in the source code.
>>
>> Kind regards
>> Andre
>
> Hi Andre,
>
>   Tried with different ports still no luck getting this error : Program exited with code -1073741819.
>
> From,
> Vino.B

Your program is crashing probably because you are dereferencing a null/ dangling pointer.
Build your program in debug mode and run it in a debugger, that way you will get a backtrace telling you the exactly line of the code when it happens.
October 21, 2019
On Saturday, 19 October 2019 at 20:40:36 UTC, Boris Carvajal wrote:
> On Saturday, 19 October 2019 at 19:08:45 UTC, Vino wrote:
>> On Friday, 18 October 2019 at 14:56:05 UTC, Andre Pany wrote:
>>> On Friday, 18 October 2019 at 09:21:46 UTC, Vino wrote:
>>>> On Friday, 18 October 2019 at 09:17:24 UTC, Vino wrote:
>>>>> [...]
>>>>
>>>> And now getting the error : Program exited with code -1073741819
>>>
>>> Hi,
>>>
>>> Maybe port 8080 is blocked, because there is an instance of your application running on the background. Just check by changing the port in the source code.
>>>
>>> Kind regards
>>> Andre
>>
>> Hi Andre,
>>
>>   Tried with different ports still no luck getting this error : Program exited with code -1073741819.
>>
>> From,
>> Vino.B
>
> Your program is crashing probably because you are dereferencing a null/ dangling pointer.
> Build your program in debug mode and run it in a debugger, that way you will get a backtrace telling you the exactly line of the code when it happens.

Hi Boris,

  I tired to build the code in debug mode and ran the program in debugger , the debugger is complaining about the line "this.conn = new Connection(connectionStr);", not sure what is wrong in the below code, hence request your help. Have the checked the connection string and the info is correct.

import vibe.vibe;
import mysql;
import std.array : array;
import std.conv;
import std.variant;

class avmtest
{
    private Connection conn;
    auto avmconnect()
    {
      auto connectionStr = "host=test.srv.com;port=3910;user=testusr;pwd=xxxx#;db=test";
      this.conn = new Connection(connectionStr);
    }
	
    auto getHostname()
    {
     ResultRange qdata = conn.query("SELECT host_name FROM `hosts_coll`");
     Row row = qdata.front;
     Variant h = row[0];
     qdata.close();
     return h.to!string;
   }
}

void hello(HTTPServerRequest req, HTTPServerResponse res)
{
 auto t = new avmtest();
 res.writeBody(t.getHostname);
}

void main()
{
  auto settings = new HTTPServerSettings;
  settings.port = 8130;
  settings.bindAddresses = ["127.0.0.1"];
  listenHTTP(settings, &hello);
  logInfo("Please open http://127.0.0.1:8130/ in your browser.");
  runApplication();
}

From,
Vino.B
« First   ‹ Prev
1 2