4

I have connected to MSSQL, but i am unaware to do a select statement and print it out. I have done this using MySQL, but unable to convert it to MSSQL. Can someone help me to find the equvalent for mysql_query,mysql_real_escape_string,mysql_num_rows,mysql_fetch_array or a sample code that helps.

// Connecting to MSSQL -  Working
$name = $_POST['myname'];
$x=mysql_query("SELECT * FROM MyTable WHERE Name='".mysql_real_escape_string($name)."'");
$num_rows = mysql_num_rows($x);    
  while($norows = mysql_fetch_array($x)) {
// PRINT ROW
}
4

2 回答 2

3

使用 mssql_* 函数

或尝试 PDO:PDO_SQLSRV 驱动程序

于 2012-09-02T17:11:14.510 回答
3

在 MSSQL 中有以下替代函数:

mysql_query              ---> mssql_query
mysql_num_rows           ---> mssql_num_rows
mysql_fetch_array        ---> mssql_fetch_array

请查看此处的官方文档以获取更多信息...

唯一缺少的函数是转义字符串(mysql_real_escape_string),为此您可以为自己定义一个像这样的函数:

function mssql_escape($str) {
    if(get_magic_quotes_gpc())
    {
        $str= stripslashes($str);
    }
    return str_replace("'", "''", $str);
}
于 2012-09-02T17:12:00.463 回答