PHP to D Conversion

zoujiaqing zoujiaqing at gmail.com
Mon Oct 21 15:29:33 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
>
> 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;
	}
}



More information about the Digitalmars-d-learn mailing list