www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - PHP to D Conversion

reply Vino <akashvino79 gmail.com> writes:
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, 

                 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 = 

	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
Oct 17 2019
next sibling parent reply Jacob Carlborg <doob me.com> writes:
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, 

                 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 = 

 	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
Oct 18 2019
parent reply Vino <akashvino79 gmail.com> writes:
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
Oct 18 2019
parent reply Vino <akashvino79 gmail.com> writes:
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); }
Oct 18 2019
parent reply Vino <akashvino79 gmail.com> writes:
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
Oct 18 2019
parent reply Andre Pany <andre s-e-a-p.de> writes:
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
Oct 18 2019
parent reply Vino <akashvino79 gmail.com> writes:
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
Oct 19 2019
parent reply Boris Carvajal <boris2.9 gmail.com> writes:
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.
Oct 19 2019
parent reply Vino <akashvino79 gmail.com> writes:
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 = 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
Oct 21 2019
parent Andre Pany <andre s-e-a-p.de> writes:
On Monday, 21 October 2019 at 11:36:00 UTC, Vino wrote:
 On Saturday, 19 October 2019 at 20:40:36 UTC, Boris Carvajal 
 wrote:
 [...]
Hi Boris, [...]
Hi, Where do you call avmconnect? Is this.conn null when connection fails? What happens if the query does not contains rows? Kind regards Andre
Oct 21 2019
prev sibling next sibling parent Aldo <aldocd4 outlook.com> writes:
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 = 

 	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).
Oct 18 2019
prev sibling next sibling parent reply zoujiaqing <zoujiaqing gmail.com> writes:
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, 

                 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 = 

 	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
import hunt.database; class avmtest { private Database db; this() { db = new Database("mysql://testusr:xxxx test.srv.com:3910/test"); } string[string][] getHostname() { string[string] data; foreach(row; db.query("SELECT host_name FROM hosts_coll")) { string[string] host; host["HostName"] = row["host_name"]; data ~= host; } return data; } }
Oct 21 2019
parent reply Andre Pany <andre s-e-a-p.de> writes:
On Monday, 21 October 2019 at 15:29:33 UTC, zoujiaqing wrote:
 On Friday, 18 October 2019 at 06:22:33 UTC, Vino wrote:
 [...]
import hunt.database; class avmtest { private Database db; this() { db = new Database("mysql://testusr:xxxx test.srv.com:3910/test"); } string[string][] getHostname() { string[string] data; foreach(row; db.query("SELECT host_name FROM hosts_coll")) { string[string] host; host["HostName"] = row["host_name"]; data ~= host; } return data; } }
Is this database library compatible with the fiber programming model of vibe-d? Kind regards Andre
Oct 21 2019
parent zoujiaqing <zoujiaqing gmail.com> writes:
On Monday, 21 October 2019 at 15:36:25 UTC, Andre Pany wrote:
 On Monday, 21 October 2019 at 15:29:33 UTC, zoujiaqing wrote:
 On Friday, 18 October 2019 at 06:22:33 UTC, Vino wrote:
 [...]
import hunt.database; class avmtest { private Database db; this() { db = new Database("mysql://testusr:xxxx test.srv.com:3910/test"); } string[string][] getHostname() { string[string] data; foreach(row; db.query("SELECT host_name FROM hosts_coll")) { string[string] host; host["HostName"] = row["host_name"]; data ~= host; } return data; } }
Is this database library compatible with the fiber programming model of vibe-d? Kind regards Andre
Yes. There are no conflicts.
Oct 21 2019
prev sibling parent zoujiaqing <zoujiaqing gmail.com> writes:
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
You can get one connection object and close it :) ```D import hunt.database; class AvmTest { private Database _db; this() { _db = new Database("mysql://testusr:xxxx test.srv.com:3910/test"); } string[string][] getHostname() { string[string][] data; auto conn = _db.getConnection(); foreach(row; conn.query("SELECT host_name FROM hosts_coll")) { string[string] host; host["HostName"] = row["host_name"]; data ~= host; } conn.close(); return data; } } ```
Oct 21 2019