PHP to D Conversion

Jacob Carlborg doob at me.com
Fri Oct 18 08:54:40 UTC 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




More information about the Digitalmars-d-learn mailing list