Examples
An example below shows only very simple code conversion. Focus on following features:
- translating C# static method invoking into PHP method invoking i.e.
MySQL.Connect
- translating C# property into PHP binary expression i.e.
connection.IsConnected
- translating C# const into PHP defined const (
Config.HOST
) or global variable (Config.USER
)
Source C# code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
public static void MyDemo() { var connection = MySQL.Connect(Config.HOST, Config.USER); if (!connection.IsConnected) { echo("Connection error <b>" + htmlspecialchars(connection.MysqlError()) + "</b>"); return; } if (!connection.SetCharset(Config.CODEPAGE_MYSQLCHARSET)) { echo("Unable to change charset <b>" + Config.CODEPAGE_MYSQLCHARSET + "</b> <b>" + htmlspecialchars(connection.MysqlError()) + "</b>"); return; } echo("Connection OK !!! "); if (!connection.SelectDb(Config.DB)) { echo("Unable to change database " + Config.DB + " <b>" + htmlspecialchars(connection.MysqlError()) + "</b>"); connection.Close(); return; } var sql = "select id, first_name, last_name from persons"; var result = connection.Query(sql); if (!result.IsOk) { echo("SQL error " + htmlspecialchars(sql) + " <b>" + htmlspecialchars(connection.MysqlError()) + "</b>"); connection.Close(); return; } echo("Sql query <b>" + htmlspecialchars(sql) + "</b> rows= <b>" + result.NumRows + "</b>." + Config.BR); Person person; while (result.FetchAssoc(out person)) { echo(htmlspecialchars(person.FirstName)); echo(htmlspecialchars(person.LastName)); echo(" "); } result.FreeResult(); } |
Converted PHP code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
public static function MyDemo() { global $MYSQL_USER; $connection = @mysql_connect(MYSQL_HOST, $MYSQL_USER); if ($connection === false) { echo 'Connection error <b>' . htmlspecialchars(mysql_error($connection)) . '</b>'; return; } if (!mysql_set_charset(CODEPAGE_MYSQLCHARSET, $connection)) { echo 'Unable to change charset <b>' . CODEPAGE_MYSQLCHARSET . '</b> <b>' . htmlspecialchars(mysql_error($connection)) . '</b>'; return; } echo 'Connection OK !!! '; if (!@mysql_select_db(MYSQL_DB, $connection)) { echo 'Unable to change database ' . MYSQL_DB . ' <b>' . htmlspecialchars(mysql_error($connection)) . '</b>'; mysql_close($connection); return; } $sql = 'select id, first_name, last_name from persons'; $result = mysql_query($sql, $connection); if ($result === false) { echo 'SQL error ' . htmlspecialchars($sql) . ' <b>' . htmlspecialchars(mysql_error($connection)) . '</b>'; mysql_close($connection); return; } echo 'Sql query <b>' . htmlspecialchars($sql) . '</b> rows= <b style="color: blue;">' . mysql_num_rows($result) . '</b>. '; while($person = mysql_fetch_assoc($result)) echo htmlspecialchars($person['first_name']) . htmlspecialchars($person['last_name']) . ' '; mysql_free_result($result); } |