Click here to Skip to main content
14,695,085 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to convert the code below from PHP to C# and need help

<!--?php function getUserHints($attr,$value)
 {    
 // configurations     
  $tableUsers = 'users';    
   $tableCols = array('id'=-->'id',
        'name'=>'name',
        'email'=>'name',
        'display'=>'display'
    );
 
    // get parameters:
    //   search table $tableUsers by column defined in $tableCols
    // if search column not found, return empty array
    if (isset($tableCols[$attr])) {
        $col = $tableCols[$attr];
        $value = mysql_real_escape_string($value);
    } else {
        return array();
    }
 
    $sql = "SELECT * FROM `$tableUsers` WHERE `$col` LIKE '$value%'";
    $res = mysql_query($sql);
 
    if (!$res) {
        return array();
    }
 
    $ret = array();
    while ($tmp = mysql_fetch_assoc($res)) {
        $ret[] = array(
            'id'=>$tmp[$tableCols['id']],
            'name'=>$tmp[$tableCols['name']],
            'email'=>$tmp[$tableCols['email']],
            'display'=>$tmp[$tableCols['display']],
            'label'=>$tmp[$tableCols['display']]."(".$tmp[$tableCols[$attr]].")",
            'value'=>$tmp[$tableCols[$attr]]
        );
    }
 
    return $ret;
}
 
if (isset($_POST['attr'])) {
    mysql_connect('localhost','dbuser','dbpass');
    mysql_select_db('dbname');
    mysql_query("SET NAMES utf8");
    $ret = getUserHints($_POST['attr'],$_POST['value']);
    echo json_encode($ret);
}
Posted
Updated 22-Aug-15 16:18pm
v2

1 solution

No, not C#, but you want to convert it to ASP.NET, and yes, you want to convert it. Not us. Understand that we do not do your work for you, we just help you in doing so.

Also, PHP and ASP.NET run under entirely opposite way, PHP was written in C/C++ and provides you with features that you can embed in your HTML, runs under Apache (Microsoft has now provided support for PHP under IIS too, for Microsoft Azure; but let's not confuse ourselves) and ASP.NET on other hand, has different frameworks in it.

1. Web Pages -- very compact framework for personal blogs
2. Web Forms -- a powerful framework for web sites, with security!
3. MVC -- the future of ASP.NET, loved by most, developed by thousands.

PHP and ASP.NET codes are entirely different, for example let me take one sample,

if (isset($_POST['attr'])) {
    mysql_connect('localhost','dbuser','dbpass');
    mysql_select_db('dbname');
    mysql_query("SET NAMES utf8");
    $ret = getUserHints($_POST['attr'],$_POST['value']);
    echo json_encode($ret);
}


In ASP.NET, you will write it this way,

if(Request["attr"] != null) { // parameter set
   // Initiate the request
   // Work around with MySQL connection
   // And write the content
}


You can find yourself a good C# guide for MySQL connections on CodeProject also, Connect C# to MySQL[^]

Finally, learning ASP.NET is also easy, just requires a little knowledge of

1. HTML, CSS and JavaScript
2. Visual Studio (or any other similar IDE)
3. C# language

You can then write ASP.NET applications, here have a look at my ASP.NET MVC beginner's tutorial, it doesn't actually require you to know ASP.NET, but will teach you 30% of ASP.NET, rest is then up to you. :-)

Understanding ASP.NET MVC using real world example, for beginners and intermediate[^]
How to connect ASP.NET website to Remote Database for CRUD operations[^]
Why use ASP.NET for web development[^]
A tip for ajax developers in ASP.NET MVC framework[^]

Good luck!
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900